Calling a script from a button

Hi! Is there a way to call a script of google sheets from a button?

I already have scripts onEdit and onChange band they are working well. But I could not find a way to call another function from a button so that no changes are needed.

Thanks a lot for your help!

@David_mrflda I have used a combination of a switch and a timed trigger. The timed trigger runs every minute and the function it calls looks at the status of the switch. If it is true it runs the script function I want and then sets the script to false.

1 Like

Thanks @George_B, I was thinking on something more intuitive like a button but since this seems not possible your proposal could work.
Is there any way I can add the switch at the bottom of a list of items?
At the end what I’m looking for is a way of deleting all the items of a list on a user action (ideally a click button, or a switch as an alternative).

@David_mrflda You would just add the switch component underneath the Inline list component. If you are not using an inline list comp but just displaying a list view of a sheet I can’t think of a way to add the switch (or a button - see below) to the bottom of it.
Theoretically you could do it with a button (open as link), but it would involve writing a Google script webapp. Which involves a bit of HTML and scripting, and possibly not worth the time involved.

There is a way to add a switch below an inline list. What you need to do is create a new sheet that contains the value that would be controlled by the switch and read by the script. That new sheet will replace the tab that you are using in your app. Change the style of the tab to Detail view, add the inline list component and point to your existing sheet, then add a switch component that points to the column in your new sheet. A checkbox component would also be a valid option. This should then work fine with onEdit/onChange events.

I thought I said that, but glad you detailed how to change from a “normal” list view to a detailed view with imbedded inline list(s).

1 Like

Hello, I accomplished it this way: I’ve put an uptade trigger to activate a script that reads a certain cell as a condition to run the code. So I used a Button in my app as “Set columns” to write in this spefic cell, making the code condition true. In the end of the code , the cell content is cleared again.

1 Like

I was going to say to do the same thing using a onChange trigger and the buton just updates a column. The trigger looks down that column for changes and creates the trigger. I actually use this in about a dozen scenarios. I use this to add formulas to rows that won’t accept arrayformulas. I use it clear separate sheets when things are submitted. I use it to trigger notifications. The list goes on and on.

1 Like

Yeah, we didn’t have the ability to attach a Set Columns action “back in the day”, lol. Our only option was using a checkbox or switch. A Button/ Set Columns action is definitely better.

1 Like

Hi Gabriel - can you share the script please that you use to clear cells?

Hello Simon.
To clear a cell you can use the following line of code in the end of your script:
For instance, this code is clearing the content of cell A1.

1 Like