Facing issues while executing the App Scripts code for Google Sheets

…衆ロ難τιáo~ 提交于 2020-08-20 14:42:59

问题


I am facing few issues with the following scripts. Here the sheet name is 'Dashboard'. First paragraph under refreshSheet() is not working fine. The issues is:

The following code is producing two protections of A1:K4 each time I run the code (Dashboard!A1:K4 and Dashboard!A1:K4). Should do only one protection each time.

Please check the codes are as follows:

   function onEdit(e){  
      if (e.range.getA1Notation() === 'C6' && e.range.getValue() === "Start 1-Period") {  
        refreshSheet();
        onePeriod();  
      }
      if (e.range.getA1Notation() === 'C6' && e.range.getValue() === "Start 2-Period") {    
        refreshSheet();
        twoPeriod();  
      }   
    }
    
    function refreshSheet() {   
      //For protecting dashboard while scripts running
      var spreadsheet = SpreadsheetApp.getActive();
      var dashboard = spreadsheet.getSheetByName("Dashboard");
      var rangem = dashboard.getRange('A1:K4');   
      var protectionm = rangem.protect();
      var me = Session.getEffectiveUser();
      protectionm.addEditor(me);
      protectionm.removeEditors(protectionm.getEditors());
      if (protectionm.canDomainEdit()) {
          protectionm.setDomainEdit(false);
      }
     
      // Other codes are here which are working fine        
    
      protectionm.remove();
 }


回答1:


You have several triggers runnig simultaneously - this is the reason why refreshSheet() is running several times.

First of all:

do NOT use the simple and installable onEdit trigger simultaneously - this leads to double execution and conflicts.

  • In your specific situation the simple onEdit trigger does not execute correctly (because it does not have the encessary permissions).

  • You should however remove the simple trigger - this can be easily done by renaming function onEdit(e) to any other name that is not a reserved key word - e.g. function Edit(e).

  • You installable trigger will still run - since you manually bound it to the function.

  • From your execution logs it seems that the installable onChange trigger runs at the same time like the onEdit trigger from which I can assume that you bound it to the same function - this makes your code execute twice.

  • Instead, in case of implementing multiple triggers, you should bind each to a different function.

  • From the code snippet you provided I cannot see your need to use an onChange trigger - maybe you it is a remainder from previous approaches.

  • Please go on Edit-> Current Projetc's triggers , review all trigger soyu have installed, delete the unnecessary ones and make sure that the remaining ones are bound to different functions.

Second:

  • To verify either the double execution of a code part is due to a double call of a function - test by running the funciton manually from the script editor.
  • For this, simply chose the correct funciton from the dropdown menu (in your case - refreshSheet() and click on the run button (the triangle).



来源:https://stackoverflow.com/questions/63251082/facing-issues-while-executing-the-app-scripts-code-for-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!