Google Apps Script try catch Handling

Asked 1 years ago, Updated 1 years ago, 37 views

We are creating a GAS that transfers the parameters entered from the Google form to the specified spreadsheet.
The spreadsheet provides edit protection to allow only certain user privileges to be written.

Application owner privileges are consistent.
At this time, when I ran the GAS, the application stopped with the following error:

 June 22, 2021 19:57:43 Error Attempting to edit protected cells and objects.If you need to edit it, contact the spreadsheet owner for protection.

is the case.So I surrounded the sentence with try catch as follows:

try{
    // your script code here
      sheetdetail.getRange(lineNumberDetail,1).setValue(applicationNumber);
      sheetdetail.getRange(lineNumberDetail, 2).setValue(detailsOfOrder);
      sheetdetail.getRange(lineNumberDetail, 3).setValue(approvalOfTheApplicationAmount);
      sheetdetail.getRange(lineNumberDetail, 4).setValue(orderPeriod);
      sheetdetail.getRange(lineNumberDetail, 5).setValue(deliveryTime);
      sheetdetail.getRange(lineNumberDetail, 6).setValue(remarks);
      sheet.getRange(lineNumber,33).setValue('Not Requested');
  } catch(e){
    MailApp.sendEmail("********", "Error report", e.message);
  }

I thought this would send an email, but it seems that the application has stopped in try.
What kind of cause can you think of here?
I would appreciate it if you could let me know if you know anything about it.
Currently

In the case of permission-related errors, we assume that trycatch is ignored and cannot be executed.

google-apps-script

2022-09-30 17:52

1 Answers

I'm not familiar with GAS, so I just looked it up a little bit, but rather than trying to write it down and doing error handling, it would be better to check if you have editing privileges before writing it.

For example, canEdit() can confirm the edit permissions for the specified range.

reference:
Check if the specified range has edit privileges: canEdit()

Returns true if you have edit permission for a specified range or false if you do not have permission.

function myFunction(){
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  constrange=sheet.getRange('B2:D10');
  range.canEdit()
}


2022-09-30 17:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.