I need a button to clear cells in a google spreads

2020-01-30 11:29发布

Im making a tool for myself with Google Spreadsheets, and as part of that tool I would like to have a button that clears a specific set of cells. As I understand it, I need to insert a drawing, and then assign a script to that drawing. Trouble is, I dont know the first thing about writing my own so, im here looking for help!

The end goal of this would be for me to have a drawing with a script attached to it that would, when activated, clear the data (make them blank, but leave the color) from cells B7-G7.

Any help you guys could offer would be fantastic!

2条回答
虎瘦雄心在
2楼-- · 2020-01-30 11:52

Such script is very simple, you should look at the tutorials to learn how to do it yourself.

Anyway, here it is:

function clearRange() {
  //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('B7:G7').clearContent();
}
查看更多
Rolldiameter
3楼-- · 2020-01-30 11:52

To add a custom menu to your Google spreadsheet, that when clicked, will list all your functions. See the code below

function onOpen() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var menubuttons = [ {name: "Clear B7-G7", functionName: "clearRange1"},
                  {name: "Clear B13-G13", functionName: "clearRange2"}];
    ss.addMenu("Custom", menubuttons);
} // note you also have to have functions called clearRange1 and clearRange2 as list below
function clearRange1() { //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('B7:G7').clearContent();
}
function clearRange2() { //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('B13:G13').clearContent();
}
查看更多
登录 后发表回答