1

I have a function that uses the onEdit trigger. It was working great both on PC and mobile yesterday, but today it no longer works on mobile but still functions on my PC. Not sure what is going on.

function onEdit(e){
if(e.range.getA1Notation() == 'D13'){
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('H5').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Maintenance Request'), true);
  spreadsheet.getRange('2:2').activate();
  spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 1);
  spreadsheet.getActiveRange().offset(0, 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Entry Form'), true);
  spreadsheet.getRange('D5').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Maintenance Request'), true);
  spreadsheet.getRange('A2').activate();
  spreadsheet.getRange('\'Entry Form\'!D5').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Entry Form'), true);
  spreadsheet.getRange('D9').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Maintenance Request'), true);
  spreadsheet.getRange('B2').activate();
  spreadsheet.getRange('\'Entry Form\'!D9').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Entry Form'), true);
  spreadsheet.getRange('D11').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Maintenance Request'), true);
  spreadsheet.getRange('C2').activate();
  spreadsheet.getRange('\'Entry Form\'!D11').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Entry Form'), true);
  spreadsheet.getRange('D13').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Maintenance Request'), true);
  spreadsheet.getRange('D2').activate();
  spreadsheet.getRange('\'Entry Form\'!D13').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Entry Form'), true);
  spreadsheet.getRange('D7').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Maintenance Request'), true);
  spreadsheet.getRange('E2').activate();
  spreadsheet.getRange('\'Entry Form\'!D7').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Entry Form'), true);
  spreadsheet.getRangeList(['D5', 'D9', 'D11', 'D13']).activate()
  .clear({contentsOnly: true, skipFilteredRows: true});
}

}
4
  • Welcome to Web Applications. This doesn't look like a practical, detailed question. Please check out tour and How to Ask. Spoiler: You should show what you have tried to learn about the problem and how to fix it, as well as have searched this site before asking the question; the question should include who you found and why it doesn't meet your needs. Please bear in mind that this is a place to collaborate and build a knowledge base, not to be used for direct "fix the code for me" requests (not showing a complete description of the problem, what was tried and the search efforts).
    – Rubén
    May 11 at 12:49
  • I kindly ask for your patience and apologize for any frustration I may convey. It is important to note that repetitive posts should be avoided as there is no benefit in repeatedly asking the same question. Originality is key in promoting a positive and rewarding experience for all.
    – Rubén
    May 11 at 12:58
  • define "no longer works" for example do you see that it ran without errors in the execution log but the changes you expected were not made? What do you mean by "on Mobile"? What platform? What browser/app name and version etc.
    – Blindspots
    May 11 at 14:29
  • Please show what you have tried to find the cause of the problem and fix the code (this is known as troubleshooting, and more specifically, in this context, as debugging); add the execution logs and be more specific about how the spreadsheet is used on mobile devices. Usually, questions should include all the relevant details to help others reproduce the problem.
    – Rubén
    May 11 at 17:05

1 Answer 1

0

TL;DR:

General

onEdit is not a "tag"; it's a reserved function name for functions to be called by a simple trigger. As a result, it's commonly known as a "simple trigger".

Performance

onEdit and other Google Apps Script features work on a "best effort" basis. This means they are not always triggered due to Google Sheets / Apps Script limitations. I.E., For editing many cells in quick succession, onEdit might not be triggered for each edited cell.

On the other hand, simple triggers had a maximum execution time of 30 seconds; the code looks inefficient as it includes many unnecessary statements for a onEdit simple trigger. These statements are usually included by the Google Sheets macro recorder because it records the user actions, which, most of the time, are inefficient compared to what the code could do.

Client - Server (device / cloud) communication

Google Sheets app for mobile devices is optimized to work offline, but Google Apps Script doesn't work offline. Suppose you need that your onEdit simple trigger works on a mobile device. In that case, you should ensure that it has a reliable Internet connection so that the communication between the spreadsheet in your device and the Google Apps Script engine be efficient. Remember that onEdit has a 30 seconds execution time limit; having a poor Internet connection or using a network very busy might make that execution longer and, in the worst case, make it fail due to exceeding the maximum execution time.

Troubleshooting

Google Apps Script has execution logs. Review the executions to learn if there is an error message logged. For example, the execution logs might tell you if their call to onEdit has exceeded the execution time limit or if there were many calls to this simple trigger, which explains why some calls not be realized, among other things.

Also, you might need to use debugging techniques like adding console.log(message) statements to the simple trigger. Again, this requires having some basic programming skills and JavaScript knowledge besides understanding the debugging and logging features of Google Apps Script.

Epilogue

Experience using Excel and VBA might help. Please remember that web applications are "light weight" compared to similar native apps for Windows, Mac, and other desktop platforms, but at the same time offer some unique advantages as data is stored in the Cloud and some parts of the software are running on powerful data centers rather than on the user device.

There are already several answers here and in Stack Overflow that explain how to make more efficient code created using the Google Sheets macro recorder and why the typically generated code shown in questions is inefficient.

Spoiler: The code in this question has a lot of activate() and read and write operations; they are reading and writing one cell at a time. The fix makes the code read and write multiple cells simultaneously, called batch operations. There is an article about this on Google Apps Script microsite in Google Developers: Best Practices.

It might be possible to use Google Sheets macro recorder to generate efficient code, but this requires one to be familiar with keyboard shortcuts and other Google Sheets features and understand what user actions are not recorded. I have found that writing a script is more efficient; at the end of the day, each person should find what works best for them.

2
  • I am used to excel VBA. So, I just recorded a macro to do what I wanted, which is just move the info from cells a,b,c,d, of one page, to the new row of the next page. The last cell, D13 is the trigger for the onEdit. put something in cell, hit enter, macro runs. On the web google sheets it runs every time. When I try and use it on mobile it just sits there. The conditions for executing the macro are not being met on the mobile side of things. what's frustrating is that it was. Again and again the code worked great. pc or phone. no issues. Now today, the mobile will not run. May 11 at 15:45
  • I understand how the code was created. Thanks for sharing that you had experience with Excel. The other part of the comment, IMO, repeats what was already said in the question. If you need further help, please edit the question to make it a better fit for this site. In the meantime, I will edit my answer to add more details, but the answer still will be general.
    – Rubén
    May 11 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.