4
votes

I'm trying to get a script that I've made for a google sheet to trigger when the sheet is opened (since I've found you can't trigger them manually through the mobile app). Somehow, I've managed to get half the script to work, while the other half does not. The script is intended for use on both desktop and mobile, and I want it to clear a predetermined set of cell ranges while also hiding a predetermined set of rows and columns (for formatting purposes). The script runs perfectly on desktop, but the clearRanges portion of the script doesn't run on mobile even though the hideRows/Columns portion of the script does. Any ideas on why this might be? The function I'm using can be seen below:

function onOpen() {
//Create custom menu options
SpreadsheetApp.getUi().createMenu('Invoicing')
    .addItem("Clear Data", "clearRange")
    .addItem("Submit", "sendEmailWithPdfAttach")
    .addToUi()

//Clear ranges
var source = SpreadsheetApp.getActiveSpreadsheet();
source.getRange('A23:A40').clearContent();
source.getRange('H6:I6').clearContent();
source.getRange('H8:I8').clearContent();
source.getRange('F13:F13').setValue('FALSE');
source.getRange('F15:F15').setValue('TRUE');
source.getRange('D19:I19').clearContent();
source.getRange('D23:G23').clearContent();
source.getRange('D31:G31').clearContent();
source.getRange('A42:I44').clearContent();
source.getRange('B24:G25').clearContent();
source.getRange('B28:G29').clearContent();
source.getRange('B32:G33').clearContent();
source.getRange('B35:G40').clearContent();
source.getRange('H24:H29').clearContent();
source.getRange('H32:H33').clearContent();
source.getRange('H35:H39').clearContent();

//Hide rows/columns
var sheets = source.getSheets();
sheets.forEach(function (s, i) {
    if (i == sheetNum) {
        s.hideRows(45, 400);
        s.hideColumns(10, 17);
    } else {
        s.hideSheet();
    }
});    
}

The custom menu options are for desktop use and can be ignored. I will also say this is my first time really using Apps Script, and most of this code was taken from elsewhere and modified. If you guys have any clue as to what I can do, I'm all ears. Thanks

2
Google Apps Scripts run on server side, so should work the same way on desktop/mobile/etc. I may assume problem is in something else - Kos
How are you triggering it from mobile. mobile only supports onEdit() - TheMaster
I guess it's possible that it isn't actually running at all, and that the rows and columns are hidden from previous use on desktop. What would be the best way to implement this using onEdit()? - Zach S
Please check this. I guess it's solve the problem. - Péter Baráth
webapps.stackexchange.com/a/87361/27487 was very helpful for me. - Ryan

2 Answers

5
votes
  • There is a simple solution to it, You can create a button on your main spreadsheet and link it with the script!

  • Open the same sheet on your Mobile Google Chrome

  • On the extreme Right Top corner, click on the three dots. A drop-down menu Opens
  • Select "View in Desktop Mode".
  • Notice, the button your Spreadsheet is now clickable, as the sheet interprets the user to be logged on to a PC
  • Now, Just Click on the button and let the script do its magic
4
votes

SpreadsheetApp.getUi() doesn't work on Google Sheets mobile apps. The alternative is to create Android add-ons for Google Sheets. Unfortunately at this time there isn't support for other mobile platform add-ons for Google Sheets.

Related Q&A

Stack Overflow

Web Applications SE