I have an Apps Script that updates columns in a Google Sheet. It is only updating unprotected columns. At the end I do a sort on all columns (including some protected columns). Those without permissions get an error from google sheets.
I tried to put a try/catch statement on the sort but that did not work. Google sheets still gives the permissions error and the code stops.
Can I either put some form of a try/catch around the sort or can I somehow see if the user has permissions before the code executes?
Here is the code that gives the Google Sheets permission error:
var wb = SpreadsheetApp.openById(id)
var ss = wb.getSheetByName(sheetname)
var sortrange = ss.getRange('A5:AM')
sortrange.sort(1)
This is the try/catch I attempted:
var wb = SpreadsheetApp.openById(id)
var ss = wb.getSheetByName(sheetname)
var sortrange = ss.getRange('A5:AM')
try {
sortrange.sort(1)
}
catch(ExceptionError) {
}
2
Answers
You need to put the api request in the
try...catch...
. Besides, there would be a syntax error ofException e
.You can modify protected ranges through
Protection
Class.canEdit()
in the documentation — of the range stopping your routine;addEditor(user)
in the documentation to do so;