Google Sheet - Exception: Request failed for https://script.google.com returned code 500

守給你的承諾、 提交于 2020-11-30 00:11:22

问题


There is an error on my sheet when I press the HIDE ROWS or SHOW ROWS button. The button works for multiple sheets as you can see on the codes below. I do have a main sheet DB_CONNECT wherein the buttons are located, then multiple sheets like MTB_Q1, MTB_Q2 and so on.. wherein it has a protected range or sheet and wherein the script that I'm using should work.

This is the error:

Exception: Request failed for https://script.google.com returned code 500. Truncated server response: <meta name="viewport" c... (use muteHttpExceptions option to examine full response)

Here's the code that I'm using

function doGet(e) {
  this[e.parameter.run](e.parameter.sheetName || null);
  return ContentService.createTextOutput();
}

function HideRows() {
  const activeSheet = SpreadsheetApp.getActiveSheet();
  const url = ScriptApp.getService().getUrl();
  UrlFetchApp.fetch(url + "?run=script_HideRows&sheetName=" + activeSheet.getSheetName(), {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});

// DriveApp.getFiles()  // This is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive.readonly". This scope is used for the access token.
}

function showRows() {
  const url = ScriptApp.getService().getUrl();
  UrlFetchApp.fetch(url + "?run=script_showRows", {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
}

var startRow = 11;
var colToCheck = 2;

// This script is the same with your "HideRows".
function script_HideRows() {
  var sheetNames = ["MTB_Q1", "MTB_Q2", "MTB_Q3", "MTB_Q4", "SUMMARY OF QUARTERLY GRADES"];  // Please set the sheet names here. In this case, 4 sheets are used.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.getSheets().forEach(sheet => {
    var sheetName = sheet.getSheetName();
    if (sheetNames.includes(sheetName)) {
      if (sheetName == "SUMMARY OF QUARTERLY GRADES") {  // When the sheet is "SUMMARY", the start row is changed.
        startRow = 12;
      }
      var numRows = sheet.getLastRow();
      var elements = sheet.getRange(startRow, colToCheck, numRows).getValues();
     
      for (var i=0; i < elements.length; i++) {
        if (shouldHideRow(sheet, i, elements[i][0])) {
          sheet.hideRows(startRow + i);
        }
      }
      // Hide the rest of the rows
      var totalNumRows = sheet.getMaxRows();
      if (totalNumRows > numRows)
        sheet.hideRows(numRows+1, totalNumRows - numRows);
    }
  });
}

// This script is the same with your "showRows".
function script_showRows() {
  // set up spreadsheet and sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
//  var ss = SpreadsheetApp.getActiveSpreadsheet(),
  var sheets = ss.getSheets();

  for(var i = 0, iLen = sheets.length; i < iLen; i++) {
    // get sheet
    var sh = sheets[i];

    // unhide columns
    var rCols = sh.getRange("1:1");
    sh.unhideColumn(rCols);

    // unhide rows
    var rRows = sh.getRange("A:A");
    sh.unhideRow(rRows);
  }
};

function shouldHideRow(ss, rowIndex, rowValue) {
  if (rowValue == "" || rowValue == '#REF!') return true; // <--- Added
  if (rowValue != '') return false;
  if (ss.getRange(startRow + rowIndex, colToCheck, 1, 1).isPartOfMerge()) return false;
  if (ss.getRange(startRow + rowIndex + 1, colToCheck, 1, 1).isPartOfMerge()) return false;
  return true;
}

What seems to be the problem here? The error only shows when I'm using it as an editor/user, but as an owner, it works fine.

The function of the HIDE ROWS button is HideRows() while the SHOW ROWS is showRows(). Below are the settings for the WebApps.

FOLLOW-UP

function HideRows() {
  const activeSheet = SpreadsheetApp.getActiveSheet();
  const url = ScriptApp.getService().getUrl();
  //UrlFetchApp.fetch(url + "?run=script_HideRows&sheetName=" + activeSheet.getSheetName(), {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
  Browser.msgBox(url + "?run=script_HideRows&sheetName=" + activeSheet.getSheetName());
  //DriveApp.getFiles();  // This is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive.readonly". This scope is used for the access token.
}

So, I tried to see what is the variable of the url. I used this script to get the value: Browser.msgBox(url + "?run=script_HideRows&sheetName=" + activeSheet.getSheetName());. So, I read somewhere to try to open the URL link and see if it works. Below is the result of this. Can this be the problem? I tried to open the url on the owner side and the doGet function seems to work. Since this code return ContentService.createTextOutput("It worked!"); works.

来源:https://stackoverflow.com/questions/65031013/google-sheet-exception-request-failed-for-https-script-google-com-returned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!