Reputation: 21
I am having problems executing scripts in Google Sheets.
I originally had installed triggers for my functions but I wanted to make copies but the triggers are gone. I set up the installable triggers in G Suite Developer Hub and now I am trying to create triggers in Script Editor of Google Sheets but I am having permission issues.
I know that I can edit auth scope in manifest file but it also didn't work.
function onOpen(e){
var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
ScriptApp.newTrigger('makeDropDownMenu')
.forSpreadsheet(ssId)
.onOpen()
.create();
}
Above is the code I want to execute and it says "You do not have permission to call ScriptApp.newTrigger. Required permissions: https://www.googleapis.com/auth/script.scriptapp at createTimeDrivenTriggers"
What should I do to install triggers in script?
Upvotes: 2
Views: 4510
Reputation: 1
My contribution is:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var sCelControlTrigger = "P3";
createTimeDrivenTriggers(sCelControlTrigger);
}
function createTimeDrivenTriggers(sCelCtlTrigger) {
var spreadsheet = SpreadsheetApp.getActive();
var nAcionadores = 0;
// Checks if triggers is created. If not, creates the triggers.
nAcionadores = ScriptApp.getProjectTriggers().length;
if (nAcionadores === 0) {
// Trigger every day 7-8 hours.
ScriptApp.newTrigger('Envia_Situacao_Saldo_Bancario')
.timeBased()
.atHour(7)
.everyDays(1)
.create();
// Trigger every day 6-7 hours.
ScriptApp.newTrigger('Gera_Programacao_Pagamentos_Automatica')
.timeBased()
.atHour(6)
.everyDays(1)
.create();
// Trigger every day 5-6 hours.
ScriptApp.newTrigger('Gera_Programacao_Pagamentos_Variaveis_Automatica')
.timeBased()
.atHour(5)
.everyDays(1)
.create();
//
spreadsheet.getRange(sCelCtlTrigger).setValue("Rotinas Automáticas Criadas");
}
}
How you can see, triggers was created if not exists any one. Triggers have specific roles to execution based in time.
Upvotes: 0
Reputation: 153
The fix is to run the onOpen
function manually once in the script editor.
Since that function would only get executed when an user opens the file, it does not get executed by your main script right away, so the permission for THAT doesn't exist yet by the time the user opens the file.
So by manually running that function on the script editor (even if it errors out), it will force a permissions prompt requesting what is needed and attach it to the script's project.
Upvotes: 1
Reputation: 3340
That type of errors are related to the oauth scopes you have. For almost all scopes, Apps script sets automatically the oauth scopes you need if you don't manually set the "oauthScopes" field in the manifest file [1]. You can either try deleting the "oauthScopes" field from the manifest and Apps Script will probably add the scopes you need, or manually add them in the manifest like this:
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets", //This one you should already have it
"https://www.googleapis.com/auth/script.scriptapp" //Thi is the one that was missing
]
[1] https://developers.google.com/apps-script/manifest/
Upvotes: 3
Reputation: 64032
You can install trigger with this. When you open the spreads go to "My Menu" and click on "Create Trigger".
function onOpen(){
SpreadsheetApp.getUi().createMenu("My Menu")
.addItem("Create Trigger","createTrigger")
.addToUi();
}
function createTrigger() {
var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
ScriptApp.newTrigger('makeDropDownMenu')
.forSpreadsheet(ssId)
.onOpen()
.create();
}
But if you just creating a menu and nothing more then you can just do that in an onOpen() simple trigger like I did above. If I'm missing your point please provide a little more direction.
Upvotes: 1