Google Apps Script – How to Create a PushButton

google sheetsgoogle-apps-script

I'm new here in this site and also new in Google Apps Script.

I'm trying to find an example to create a Pushbutton inside Google sheets.

I've found something saying it is "deprecated".

What I expect to do is putting a button called "Move to Sent" that when pressed must count some lines till the line saying "END" and then move the content of those lines to another location in the same sheet.

It looks quite simple to me but I've got stuck in the very beginning.

Some help would be appreciated at least to point me in the right direction to find out.

Best Answer

As said in the deprecation message you found, user interfaces can be created with the HTML Service.

But if you just want to create a button that is displayed in your sheet, there is an easier way.

Click Insert → Drawing and draw a text box. Give it a background color and a border, so that it looks something like this:

A text box looking like a button

Make sure you position the text box to the very top left corner of the drawing editor window.

When you're satisfied, click the Save and close button to return to your spreadsheet. The button (your drawing) is now positioned on your spreadsheet. You can click and move it anywhere you want.

Now click the button to select it, and click the little down-pointing triangle and select Assign script:

Button positioned on spreadsheet

In the prompt that pops up, assign a function name to your button, e.g. clickMe.

Next, click Tools → Script Editor to open the Script Editor window. Here, define your function clickMe (or whatever you entered in the Assign script popup):

function clickMe() {
   // Here, implement your script logic, e.g.:
   Browser.msgBox("You clicked me");
}

Click the Save button (give your script file a name when prompted) and return to your spreadsheet. Try to click the button. The first time you do so, you will be asked to give the script permission to run, and to access your spreadsheets. Having done so once, clicking the button will show a popup saying You clicked me.

I have created an example spreadsheet to demonstrate, feel free to copy it. You have to be logged in order for the button to work.