How can I create a button using app script and assign a script to it?

Viewed 1670

As per my understanding if I want to create a button in google sheets, I have to access draw and create a shape like button, then link it to the script i have as it's described in here and in How do you add UI inside cells in a google spreadsheet using app script?

I've three questions,

  1. Is there a way to create a button and its onClick function inside app script ?
  2. If I used the method above (drawing, assigning ...) is this will be accessible to others who are going to use the same script (as i'm creating an Add-on)
  3. What's is the use of SpreadsheetApp.getUi().Button?
2 Answers
  1. Yes. But not with drawing. You can use any external images as buttons. Use sheet.insertImage() and assignScript on the returned overGridImage.

  2. Yes.

  3. No such method. But you can use alerts

function buttonOnADialog() {
  var html='<input type="button" value="Close" onclick="google.script.host.close();" />';
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Title");
}
Related