Google Script : 실패한 요청 및 반환 된 코드 500

Nov 27 2020

라는 오류가 발생했습니다 code 500. 소유자 계정에서 스크립트를 사용하는 경우 스크립트가 제대로 작동하지만 사용자 / 편집자로 파일을 열면 code 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;
}

답변

1 contributorpw Nov 28 2020 at 04:23

먼저 주석 처리

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

둘째, 이것을 피하십시오

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

대체하다

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

셋째, 코드를 변경할 때마다 액세스하는 모든 사용자를 위해 웹 앱을 게시합니다.

다음 코드가 잘 작동합니다.

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);
}