Google Sheets Button Event Trigger only work on Desktop Web, but it doesn't work on Mobile App (Google Drive/Google Sheets App).
To make this work, we need to create a dropdown menu in a particular cell, then listen to onEdit
changed on this cell and perform the appropriate event trigger.
Create Dropdown
- Select a cell
- Menu:
Data -> Data Validation
- For
Criteria
, selectList of items
. Enter items separated by command:LAUNCH,None
. (I only have one command currently, which isLAUNCH
). - Click
Save
.
Listen to Dropdon Edit
Menu: Tools -> Script Editors
and add the following functions.
function onEdit(e) { if (e.range.getA1Notation() == 'B2') { Logger.log(e.value); if (e.value == 'LAUNCH') { Logger.log('onClick'); // do something e.range.clear(); // optional } }}
NOTE: This solution will work for simple tasks, but it would not work for tasks which require special permission, like make HTTP request or send email. Refer to the next section for installable triggers.
Installable Triggers
We need Installable Triggers
create a trigger event which can make HTTP request.
Menu: Tools -> Script Editors
and add the following functions.
function createEditTrigger() { var ss = SpreadsheetApp.getActive(); var triggers = ScriptApp.getUserTriggers(ss); var isTriggerExist = false; for each (t in triggers) { if (t.getHandlerFunction() == 'onTriggerEdit') { isTriggerExist = true; } } if (!isTriggerExist) { ScriptApp.newTrigger('onTriggerEdit') .forSpreadsheet(ss) .onEdit() .create(); }}function onTriggerEdit(e) { if (e.range.getA1Notation() == 'B2') { Logger.log(e.value); if (e.value == 'LAUNCH') { Logger.log('onClick'); onClick(); e.range.clear(); } }}
You need to call createEditTrigger
on Desktop (not Mobile).
- You could run the function in Script Editors: Click on
Select function -> createEditTrigger
then click on theRun
icon. - You can create a button on Google Sheets which execute createEditTrigger function.
You shall be prompted with a permission request screen.
After executing createEditTrigger
once on Desktop (this only need to be run once per lifetime of this spreadsheet), you can click on the dropdown on mobile devices to trigger an event which can make HTTP request.
References: