Home > database >  Why is this Web App showing an error "Script function not found: events"?
Why is this Web App showing an error "Script function not found: events"?

Time:11-21

The user clicks on a button, which has main() assigned to it.

It runs with no errors, but the protections are not removed or applied as it seems to get stuck because of the error mentioned in the question.

Here are the functions borrowed and adapted from Stackoverflow:

// This is the main function. Please set this function to the run button on Spreadsheet.
function main() {
  //DriveApp.getFiles(); // This is a dummy method for detecting a scope by the script editor.
  const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const sheetName = activeSheet.getName();

  if (sheetName === 'Itens_para_Costurar') {
    var token = ScriptApp.getOAuthToken()
    var url = ScriptApp.getService().getUrl();

    var options = {
      'method': 'post',
      'headers': { 'Authorization': 'Bearer '   token },
      muteHttpExceptions: true
    };

    UrlFetchApp.fetch(url   "&key=removeprotectListaCortadas", options); // Remove protected range

    listaPecasCortadas();//Atualiza a lista de peças cortadas

    SpreadsheetApp.flush(); // This is required to be here.
    UrlFetchApp.fetch(url   "&key=addprotectListaCortadas", options); // Add protected range
  }
}

function doGet(e) {
  if (e.parameter.key == "removeprotectListaCortadas") {
    var listaPecasCortadasSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Itens_para_Costurar'); // Please set here.

    // Remove protected range.
    var protections = listaPecasCortadasSht.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    for (var i = 0; i < protections.length; i  ) {
      console.log('Protection Name: '   protections[i].getDescription())
      protections[i].remove();
    }
  } else {
    // Add protected range.
    var ownersEmail = Session.getActiveUser().getEmail();
    var protection = listaPecasCortadasSht.getRange('A8:J100').protect();
    var editors = protection.getEditors();
    for (var i = 0; i < editors.length; i  ) {
      var email = editors[i].getEmail();
      if (email != ownersEmail) protection.removeEditor(email);
    }
  }
  return ContentService.createTextOutput("ok");
}

I would appreciate any light towards the solution.

CodePudding user response:

Modification points:

  • If you were using my answer, when I posted it, ScriptApp.getService().getUrl() returned the endpoint of the developer mode. But, in the current stage, it doesn't return the endpoint of the developer mode. And, in this case, the invalid endpoint is returned. Ref

  • In your request body, 'method': 'post', is used. But, in your script, doGet is used. I thought that this might be the reason for your current issue.

  • In your script, var url = ScriptApp.getService().getUrl(); is used. But, using url, you are using UrlFetchApp.fetch(url "&key=addprotectListaCortadas", options);. In this case, the query parameter is not correct.

When these points are reflected in your script, how about the following modification?

From:

var token = ScriptApp.getOAuthToken()
var url = ScriptApp.getService().getUrl();

var options = {
  'method': 'post',
  'headers': { 'Authorization': 'Bearer '   token },
  muteHttpExceptions: true
};

UrlFetchApp.fetch(url   "&key=removeprotectListaCortadas", options); // Remove protected range

To:

var url = "###"; // Please manually set your Web Apps URL here.
var options = { muteHttpExceptions: true };
UrlFetchApp.fetch(url   "?key=removeprotectListaCortadas", options);
  • If your Web Apps is deployed as Execute the app as: Me and Who has access to the app: Anyone, when the Web Apps URL is used, the access token is not required to be used. If you want to use the developer mode, please use the access token.

Note:

  • This answer is for your current issue. So, I cannot check your all script. Please be careful about this.

  • When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.

  • You can see the detail of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".

  • Related