问题
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
onEdittrigger 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 triggerruns at the same time like theonEdittrigger 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
onChangetrigger - 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