Sunday, July 27, 2025

🟢34 Running Google Apps Script Functions from Google Sheets Mobile App with a Dropdown MenuAdded34

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() {
  // Function now empty, can be customized for PDF export logic if needed
}
  

2. Create a Dropdown Menu in Cell C3

- On your sheet (e.g., "Sheet1"), select cell C3.
- On the mobile app, tap the menu (three dots) in the top-right corner, then select Data validation (as shown in the image).
- Set the criteria to "List of items" and enter the function names: clearItems, clearAll, exportPdf (separate each with a comma).
- Save the validation to create a dropdown menu in C3. You can now 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 from the dropdown (e.g., clearItems, clearAll, or exportPdf), and press "Done" or the return key.
- The selected function will execute automatically:
- clearItems clears content from A5:A100.
- clearAll clears content from A5:E100.
- exportPdf is currently empty but can be customized for PDF export logic.
- The cell will clear after execution.

4. Notes

- Ensure the sheet name in the script matches your active sheet.
- The dropdown can now be set up directly on the mobile app using the Data validation option.

No comments:

Post a Comment