Warm tip: This article is reproduced from serverfault.com, please click

其他-Google脚本:请求失败并返回了代码500

(其他 - Google Script: Failed request and returned code 500)

发布于 2020-11-28 00:29:25

我遇到一个名为的错误code 500如果我在所有者帐户中使用脚本,该脚本可以正常运行,但是如果我要以用户/编辑者身份打开该文件,则会code 500显示错误。这是我正在处理的示例电子表格链接我尝试在这里询问但似乎有点复杂,所以我创建了一个新的单个电子表格,以便可以轻松识别错误。

代码500

这是代码

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

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()}});
  Browser.msgBox(url + "?run=script_showRows");
}

var startRow = 6;
var colToCheck = 2;

// This script is the same with your "HideRows".
function script_HideRows() {
  var sheetNames = ["MS_Q1", "MS_Q2", "MS_Q3", "MS_Q4", "SUMMARY"];  // 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") {  // When the sheet is "SUMMARY", the start row is changed.
        startRow = 7;
      }
      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 != '') 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;
}
Questioner
cjvdg
Viewed
0
contributorpw 2020-11-28 12:23:39

首先,将其注释掉

// this [e.parameter.run] (e.parameter.sheetName || null);

其次,避免这种情况

const url = ScriptApp.getService().getUrl();

替换为

const url = 'https://script.google.com/macros/s/ABCD1234/exec';

第三,每次更改代码时,发布供所有用户访问的Web应用程序

在此处输入图片说明

下一个代码对我有用

function doGet(e) {
  return ContentService.createTextOutput('It worked!');
}

function HideRows() {
  const activeSheet = SpreadsheetApp.getActiveSheet();
  const url =
    'https://script.google.com/macros/s/ABCD1234/exec';
  var response = UrlFetchApp.fetch(
    url + '?run=script_HideRows&sheetName=' + activeSheet.getSheetName(),
    {
      headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
      muteHttpExceptions: true,
    }
  );

  Browser.msgBox(response);
}