Saturday, July 26, 2025

Running Google Apps Script Functions on Google Sheets Mobile App with a Dropdown Menu

You can execute custom functions directly from your Google Sheet using the onEdit trigger, even on the mobile app! Follow these steps to set it up, including adding a dropdown menu for function names:

1. Set Up Your Google Sheet

- Open your Google Sheet and name the sheet where you want to run the functions (e.g., "Sheet1").
- Go to Extensions > Apps Script on a desktop, paste the following code, and save it:

Full Script to Review:

function onEdit(e) {
  var sheetName = 'Sheet1'; // Set the sheet name here
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  if (e.range.getA1Notation() == 'C3' && sheet) {
    if (/^\w+$/.test(e.value)) {        
      eval(e.value)();
      e.range.clear();
    }
  }
}

// Example functions
function clearItems() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  sheet.getRange("A5:A100").clearContent();
}

function clearAll() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  sheet.getRange("A5:E100").clearContent();
}

function exportPdf() {
  SpreadsheetApp.getUi().alert("Export to PDF function triggered!");
}
  

2. Create a Dropdown Menu in Cell C3

- On your sheet (e.g., "Sheet1"), select cell C3.
- On a desktop, go to Data > Data Validation.
- Set the criteria to "List of items" and enter the function names: clearItems, clearAll, exportPdf (separate each with a comma).
- Check "Show dropdown list in cell" and save.
- This creates a dropdown menu in C3. On the mobile app, tap C3 to access the dropdown.

3. Using the Mobile App

- Open the Google Sheets app on your mobile device and load the same sheet.
- Tap cell C3, select a function name by taping(e.g., clearItems, clearAll, or exportPdf), a
- The selected function will execute automatically:
- clearItems clears content from A5:A100.
- clearAll clears content from A5:E100.
- exportPdf triggers a confirmation alert (note: some UI features like alerts may not work fully on mobile).
- The cell will clear after execution.

4. Notes

- Ensure the sheet name in the script matches your active sheet.
- Some advanced features (e.g., UI alerts) may have limited functionality on mobile.
- The dropdown must be set up on a desktop; it will sync to the mobile app.

Test it out and let me know how it works for you! For more details, explore Google Apps Script documentation.

No comments:

Post a Comment