Google Script: échec de la demande et renvoi du code 500

Nov 27 2020

Je rencontre une erreur appelée code 500. Le script fonctionne bien si je l'utilise dans le compte propriétaire, mais si je vais ouvrir le fichier en tant qu'utilisateur / éditeur, l' code 500erreur s'affiche. Voici le lien vers l'exemple de feuille de calcul sur lequel je travaille. J'ai essayé de demander ici mais il semble que c'est un peu compliqué, j'ai donc créé une nouvelle feuille de calcul unique afin qu'elle puisse être facilement identifiée l'erreur.

Voici le code

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

Réponses

1 contributorpw Nov 28 2020 at 04:23

Tout d'abord, commentez ceci

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

Deuxièmement, évitez cela

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

remplacer à

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

Troisièmement, publiez l'application Web pour tous les utilisateurs accédant à chaque fois que vous changez de code

Le code suivant fonctionne bien pour moi

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