I need a button to clear cells in a google spreadsheet [closed]
Asked Answered
S

2

18

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!

Sherlene answered 13/2, 2012 at 21:50 Comment(0)
T
30

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();
}
Tactician answered 14/2, 2012 at 0:14 Comment(4)
Thanks for the reply ^.^ I was trying to teach myself, but I just honestly dont get programming. I have the script set up in the editor, and it works just fine. BUT, I cant seem to get it to assign to one of the buttons that I drew. Im clicking the button, selecting "assign Script" and then typing the name of the script into the box that comes up. When I hit ok, and then click the button with the script assigned, I get an error that says "Script function CAC could not be found" CAC is the name of the script.Sherlene
You should write the function name, not the script name. In my example clearRangeTactician
facepalm Thank you so much guys. Im a total noob here. One more question, and then all is right with the world. How do I go about setting up different buttons to clear different ranges? For example, I now have my button set to clear B7 to G7 thanks to you awesome people, but I need another now to clear B13-G13. I made a second script, but they are function named "clearRange"Sherlene
I don't need to create a second "script". Just another function in the same script. Copy this function, change the range to the desired one, and rename the function to something like clearRange2.Tactician
P
11

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();
}
Petes answered 29/10, 2012 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.