Google स्क्रिप्ट: असफल अनुरोध और कोड 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);
}