I have a spreadsheet with a collection of cards, and I wrote a doGet()
function to pick a random one, and return it to the user. When entering the Web App url into a web browser, the result appears fine. However, when I try to make a HTTP Get request through the request module, it returns this:
<!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">We're sorry, a server error occurred while reading from storage. Error code INTERNAL.</div></body></html>
Extracted error message is:
We're sorry, a server error occurred while reading from storage. Error code INTERNAL.
I googled this error code, but nobody's had the same ending of "Error code INTERNAL", and most were about other users. Here is my doGet function and request function
Google Apps Script:
function doGet(request){
var requestType = request.parameter.type
if(requestType == "random_card"){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var entriesL = sheet.getLastRow() - 1;
var cardNum = getRandomInt(entriesL);
var card = SpreadsheetApp.getActiveSheet().getRange(2 + cardNum, 2).getValue();
var set = SpreadsheetApp.getActiveSheet().getRange(2 + cardNum, 1).getValue();
var output = '{"card":"' + card + '","cardSet":"' + set + '"}';
Logger.log(output)
return ContentService.createTextOutput(output);
}
}
Node.js
function randomCard(){
var info;
request.get('{url here}?action=get&type=random_card',function(err,res,body){
console.log(body)
info = JSON.parse(body);
});
return info;
}
getActiveSpreadsheet()
- it is not available in web app context (although docs say that functions "can get a reference" with it, by "spreadsheet context" they mean "document-bound scripts" only). – MeteoricgetActiveSpreadsheet
) - try opening the web app in script editor, run the function in question and authorize it. If you already did so, check if the web app is configured to run "as me" (if you are ok with that) or consider switching to the Sheets API – Meteoric/dev
path (you most likely know that, just eliminating the most obvious) and update the deployed version if so. Please, do let me know if the issue persists - as you've noticed, error messages in GAS are not very helpful by themselves – Meteoric