Permissions API Call Failing when used in onOpen simple trigger - google-apps-script

I've created a Google Sheet with an Apps Script to do issue and task tracking. One of the features I'm trying to implement is permissions based assigning of tasks. As a precursor to that, I have a hidden sheet populated with a list of users and their file permissions, using code similar to that in this StackOverflow question
When I manually run the code, it works fine. However, I want it to load every time the sheet is opened in case of new people entering the group or people leaving the group. So I made a call to my function in my onOpen simple trigger. However, when it is called via onOpen, I get the following:
GoogleJsonResponseException: API call to drive.permissions.list failed with error: Login Required
at getPermissionsList(MetaProject:382:33)
at onOpen(MetaProject:44:3)
Here are my functions:
My onOpen Simple Trigger:
function onOpen() {
//Constant Definitions for this function
const name_Access = 'ACCESS';
const row_header = 1;
const col_user = 1;
const col_level = 2;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sht_Access = ss.getSheetByName(name_Access);
var ui = SpreadsheetApp.getUi();
ui.createMenu('MetaProject')
.addSubMenu(
ui.createMenu('View')
.addItem('Restore Default Sheet View', 'restoreDefaultView')
.addItem('Show All Sheets', 'showAllSheets')
)
.addSeparator()
.addToUi();
//Clear Contents, Leave Formatting
sht_Access.clearContents();
//Set Column Headers
var head_name = sht_Access.getRange(row_header,col_user);
var head_level = sht_Access.getRange(row_header,col_level);
head_name.setValue('User');
head_level.setValue('Level');
//Refresh User List for use in this instance
getPermissionsList();
}
Here is getPermissionsList:
function getPermissionsList() {
const fileId = "<REDACTED>"; // ID of your shared drive
const name_Sheet = 'ACCESS';
const row_start = 2;
const col_user = 1;
const col_access = 2;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var thissheet = ss.getSheetByName(name_Sheet);
// THIS IS IMPORTANT! The default value is false, so the call won't
// work with shared drives unless you change this via optional arguments
const args = {
supportsAllDrives: true
};
// Use advanced service to get the permissions list for the shared drive
let pList = Drive.Permissions.list(fileId, args);
//Put email and role in an array
let editors = pList.items;
for (var i = 0; i < editors.length; i++) {
let email = editors[i].emailAddress;
let role = editors[i].role;
//Populate columns with users and access levels / role
thissheet.getRange(row_start + i,col_user).setValue(email);
thissheet.getRange(row_start + i,col_access).setValue(role);
}

I searched before I posted and I found the answer via some related questions in the comments, but I didn't see one with an actual answer. The answer is, you cannot call for Permissions or anything requiring authorization from a Simple Trigger. You have to do an Installable trigger.
In order to do that do the following:
Create your function or rename it something other than "onOpen".
NOTE: If you name it onOpen (as I originally did and posted in this answer) it WILL work, but you will actually run TWO triggers - both the installable trigger AND the simple trigger. The simple trigger will generate an error log, so a different name is recommended.
Click the clock (Triggers) icon on the left hand side in Apps Script.
Click "+ Add Trigger" in the lower right
Select the name of your function for "which function to run"
Select "onOpen" for "select event type"
Click "Save".
The exact same code I have above now runs fine.

Related

Change from manually triggered script to time-driven trigger using Google Apps script

I have an app script for Google Sheets that works when I trigger it manually, but I would like it to be time driven, running automatically once an hour. I've tried setting that up using the Apps Script UI, and it looked like this:
Trigger
But I consistently get this error message:
Exception: Cannot call SpreadsheetApp.getUi() from this context.
at unknown function
I also tried writing the time trigger into the script, but kept getting an error. Here's the current script, which does work fine when I trigger it manually.
var ui = SpreadsheetApp.getUi();
function onOpen(e){
ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi();
}
function getGmailEmails(){
var label = GmailApp.getUserLabelByName('EmailsToBeExported');
var threads = label.getThreads();
for(var i = threads.length - 1; i >=0; i--){
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++){
var message = messages[j];
extractDetails(message);
}
threads[i].removeLabel(label);
}
}
function extractDetails(message){
var dateTime = message.getDate();
var subjectText = message.getSubject();
var senderDetails = message.getFrom();
var bodyContents = message.getPlainBody();
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]);
}`
You can not use SpreadsheetApp.getUi() from a time driven trigger. Change you triggers to:
You can also maintain the usability by altering the code like so:
try {
const UI = SpreadsheetApp.getUi();
} catch (err) {
const UI = {
alert: function() {return;},
prompt: function() {return true;},
button: {YES: false},
ButtonSet: {YES_NO: false}
}
}
You'll have to set parameters for the UI manually, but what it does it makes it so that your code runs and handles things as you expect.
I specifically put the prompt as true and the buttons as false. That way, if I am checking if they are equal to a specific button, it will always evaluate false.
e.g.
var result = UI.prompt("Some title", "Some question", UI.ButtonSet.YES_NO);
if (result == UI.Button.YES) {//always false
//This part does not evaluate
}
You'd have to make sure that you can do this consistently throughout your script. You may like to change the assignments of the values (depending on your conditional statements) to strings, numbers, or other unique things, or change your conditional statements to include "===" instead, etc. The prompts are the trickiest ones. With this change in code, you can have the UI prompts and alerts when you run the script from within Google Sheets. It will skip them when you run it from outside, such as timed executions or other things that would activate your script externally.

Creating a view history log every time someone accesses a google doc and/or copies it using apps script

I wrote a script to log who and when opens a google doc. It works fine but the doc is a template for users to fill in so want to ensure whoever needs it will make a copy of the template through apps script.
I know users need edit access for the script to work but in terms of workarounds:
Is there a way for the script to still work if I give them a copy link or a template link?
Does it make more sense to use the google sheet as the base and pull from the google doc ID?
If that doesn't work than:
2) Is there a way to prompt them with a menu window to copy the file but delay it by x seconds on open?
//setups a count for the file
function setup() {
var propertyService = PropertiesService.getDocumentProperties();
propertyService.setProperty('viewCount', '0');
}
//logs the email and date of the user accessing the file
function onOpen(e) {
var count = parseInt(PropertiesService.getDocumentProperties().getProperty('viewCount'))+1;
PropertiesService.getDocumentProperties().setProperty('viewCount', count);
Logger.log(count);
var sheet = SpreadsheetApp.openById([spreadsheet ID]);
var user = Session.getActiveUser().getEmail();
var date = new Date();
sheet.getSheetByName('View Count').appendRow([user,date]);
}
You can add a menu item that creates a copy of the template for the user, logs the copy to the same sheet where you track who opens the file, and prompts the user to open their copy of the file to start working.
Steps
First: add these 3 functions.
The first, copyFile, creates a copy of the active document and names it the same plus the user's email. Then it logs the activity to the same change log, adding the new file's ID to the row. Finally, thanks to this nifty function it pops up a window with a link to the new file so the user can easily open it and start working.
The second, showAnchor, is the function that generates the HTML dialog box which presents the user a link to their new file.
The third, createMenu, adds a new menu item to the Google Docs nav bar, which prompts the user to copy the document. This, of course, triggers the copyFile function.
function copyFile() {
var thisDoc = DocumentApp.getActiveDocument()
var sheet = SpreadsheetApp.openById([spreadsheet ID]);
var user = Session.getActiveUser().getEmail();
var date = new Date();
var newCopy = DriveApp.getFileById(thisDoc.getId()).makeCopy(thisDoc.getName() + " " + user)
sheet.getSheetByName('View Count').appendRow([user,date,newCopy.getId()]);
var url = newCopy.getUrl()
showAnchor('Open Your File',url);
}
function showAnchor(name,url) {
var html = '<html><body>'+name+'</body></html>';
var ui = HtmlService.createHtmlOutput(html)
DocumentApp.getUi().showModelessDialog(ui," ");
}
function createMenu() {
const ui = DocumentApp.getUi();
const menu = ui.createMenu("Copy This Template");
menu.addItem("Copy", "copyFile");
menu.addToUi();
}
Then: add a call to createMenu() at the end of your onOpen script:
function onOpen(e) {
var count = parseInt(PropertiesService.getDocumentProperties().getProperty('viewCount'))+1;
PropertiesService.getDocumentProperties().setProperty('viewCount', count);
Logger.log(count);
var sheet = SpreadsheetApp.openById([spreadsheet ID]);
var user = Session.getActiveUser().getEmail();
var date = new Date();
sheet.getSheetByName('View Count').appendRow([user,date]);
createMenu();
}

Google sheet sends an email when a new form is submitted if a question has a particular answer

I hope you can help, I have tried to find a solution but I am unable to find one that resolves my issue.
I have a form that collects information if a student request a change to their course, we are a multi school site and depending on the site, a different member of staff is required to approve the change.
Part of the information collect is the site code OAAd for example, and this information is held in column F.
If a submission is received the 'On form submit' trigger, triggers the script. Currently it doesn't seem to work, yet it triggers without error. If I change the trigger 'On edit' and edit a cell in column F it works fine.
eventually I will use ifElse to add in the additional options for each site, but if I could get it to work for one site that would be great.
function sendEmailapproval(e) {
if(e.range.getColumn()==6 && e.value=='OAAd'){
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email Addresses").getRange("B3");
var recipent = emailRange.getValue();
var html = HtmlService.createTemplateFromFile("Email1.html");
var htmlText = html.evaluate().getContent();
var subject = "New Change Log Request Post 16";
var body = "";
var options = { htmlBody: htmlText}
MailApp.sendEmail(recipent, subject, body, options)
}
}
Background
There are 2 options to add an onFormSubmit trigger:
Using the form settings / script.
Using the attached spreadsheet script.
Both methods gives an event object to communicate with new submmited responese, but threre are differaces between those objet so you need to dicide in advance which method to choose.
You can read more about this topic here.
For your propose, would preder to use the spreadsheet script, since it easier to extract specific values from it.
Solution
First, Here is your correct sendEmailapproval function:
function sendEmailapproval(e) {
const namedValues = e.namedValues
const question = 'Question 3'; // copy-paste from the form
switch (namedValues[question][0])
{
case 'OAAd':
const emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email Addresses").getRange("B3");
const recipent = emailRange.getValue();
const html = HtmlService.createTemplateFromFile("Email1.html");
const htmlText = html.evaluate().getContent();
const subject = "New Change Log Request Post 16";
const body = "";
const options = { htmlBody: htmlText}
MailApp.sendEmail(recipent, subject, body, options)
break;
// case 'SOMETHING_ELSE':
// break;
default:
}
}
Note that e.namedValues is an object, where each key mapped to an array.
So namedValues[question][0] refers to the string value of namedValues[question].
Second, add a trigger to the spreadsheet that attached to the form.
function createTrigger() {
const sheet = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").getSheetByName("THE_SHEET_NAME");
ScriptApp.newTrigger("sendEmailapproval")
.forSpreadsheet(sheet)
.onFormSubmit()
.create();
}

Google Scripts + Sheets + Calendar "You do not have permission to call getCalendarsByName"

I have a google script with the following code:
function hoursBetween(fromDate, toDate) {
var dates = initializeDates();
var from = dates[fromDate];
var to = dates[toDate];
var workCals = CalendarApp.getCalendarsByName('Work');
var workCal = workCals[0];
var events = workCal.getEvents(from, to);
var predictedMonthlyHours = 0;
for (var i = 0; i < events.length; i++) {
var event = events[i];
var startTime = event.getStartTime();
var endTime = event.getEndTime();
var duration = endTime - startTime;
duration = parseFloat(duration/(3600*1000));
duration -= dates['break'];
predictedMonthlyHours += duration;
}
return predictedMonthlyHours;
}
I have ran in the script editor with no problems (it asked for auth and I accepted)
When I try to call the function from my spreadsheet I get the following error:
"You do not have permission to call getCalendarsByName"
I have also tried adding triggers with no result.
How are you trying to call from spreadsheet?
If you're trying to call via a spreadsheet formula =hoursBetween(), then it won't work. See Custom Functions..."Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data,"
Other options to set the cell value:
manual button click in a sidebar Add-on
manual click on a custom menu option
install a timed trigger to run automatically
Because the method is looking for multiple "Calendars" it's returning an array of calendars. So if you have only one calendar named "Work", you still need to call it and pick which one it is. So try changing the variable "workCals" to `
var workCals = CalendarApp.getCalendarsByName('Work')[0];
Adding the [0] will pick the first one available.
With out seeing the rest of the code, hard to decipher.

Allow script to run as admin to update spreadsheet

I have a spreadsheet that is shared with some other users. Many of the cell are range protected. However, through a menu I allow a user to run a script (which access an external library therefore invisible and not in the control of the user) that will modify some of the protected ranges. However, the script throws that there is no permission to perform this operation.
Is there any option to have this library run with library 'admin' rights so it doesn't throws due to protection?
Thx!
According to the documentation : "the library does not have its own instance of the feature/resource and instead is using the one created by the script that invoked it. "
So library is not the way to go.
You can achieve that behavior using a standalone script that would run as a service (a doGet() function in a deployed webapp) that you would deploy as "running as you" and that you would call with parameters to tell it what to do on the target spreadsheet range.
Edit : in its most basic implementation you can use a simple script like this one as a server app :
function doGet(e) {
if(e.parameter.mode==null){return ContentService.createTextOutput("error, wrong request").setMimeType(ContentService.MimeType.TEXT)};
var coord = e.parameter.coord;
var mode = e.parameter.mode;
var value = e.parameter.value;
var ss = SpreadsheetApp.openById('11myX1YX_________________FS6BesaBEnQ');
var sh = ss.getSheetByName(e.parameter.sN);
if(mode=='r'){
var sheetValue = JSON.stringify(sh.getRange(coord).getValue());
var valToReturn = ContentService.createTextOutput(sheetValue).setMimeType(ContentService.MimeType.JSON);
return valToReturn;
}
if(mode=='w'){
sh.getRange(coord).setValue(value);
return ContentService.createTextOutput(value).setMimeType(ContentService.MimeType.JSON);
}
return ContentService.createTextOutput('error').setMimeType(ContentService.MimeType.TEXT);
}
The above script should be deployed with the following parameters :
Then you can use it with a simple urlFetch like below :
var url = "https://script.google.com/macros/s/AKfycbxs9M0ib-VRmmcVJ0UUJXmHITOrWcoG8bYrK4EK7Tvl0krzsYc/exec"
function testServerLink(){
var coord = 'A3';//coordinates in A1 notation
var sheetName = 'Sheet1';
var data = 'test value';
var mode = 'w';// w for "write" and r for "read"
var write = sheetService(mode,coord,sheetName,data);
Logger.log(write);//shows the result in logger
var read = sheetService('r','A1',sheetName,data);
Logger.log(read);//shows the value that was in A1 cell
}
function sheetService(mode,coord,sheetName,data){
Logger.log(url+"?mode="+mode+"&coord="+coord+"&sN="+sheetName+"&value="+data);// shows the actual url with parameters, can be tested in a browser
var result = UrlFetchApp.fetch(url+"?mode="+mode+"&coord="+coord+"&sN="+sheetName+"&value="+data);
return result
}