2

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?
Doom
  • 206
  • 1
  • 4
  • 14
  • 1
    Is there a reason you'd want a drawn button in your add-on instead of using a custom menu or HTML interface? A drawing would be editable by the end user, thus breaking your add-on. – Diego Sep 07 '20 at 15:49
  • I agree with you, that's why I'm asking for another way rather than drawing. – Doom Sep 07 '20 at 15:52
  • https://developers.google.com/gsuite/add-ons/concepts/html-interfaces – Diego Sep 07 '20 at 15:53

2 Answers2

4
  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

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • So by adding image as a button, this will be accessible to other users right, and they won't be able to modify it ? – Doom Sep 07 '20 at 17:16
  • @Doom Any user with edit access can modify it or even remove it – TheMaster Sep 07 '20 at 17:17
  • Aha, so i think making menus is safer and easier, but i wondered if i can make something that's visible to users on the sheet rather than menus and do the same functionality. – Doom Sep 07 '20 at 17:19
  • @Doom You can check if the button is modified or just add the button `onOpen`, if not present. Yes menus are better. – TheMaster Sep 07 '20 at 17:21
0
function buttonOnADialog() {
  var html='<input type="button" value="Close" onclick="google.script.host.close();" />';
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Title");
}
Cooper
  • 59,616
  • 6
  • 23
  • 54