1

I am posting data to google sheets using php, everytime I post the data it get added on the sheet correctly, but i get the error message as

We8d#39;re sorry, a server error occurred. Please wait a bit and try again.

My php curl post code is:

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $gsurl);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 $output = curl_exec($ch);
 $info = curl_getinfo($ch);
 curl_close($ch);

$res = json_decode($output, 1);

My GS App Script is :

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service


function doGet(e){
  return handleResponse(e);
}

function doPost(e){
  return handleResponse(e);
}

function handleResponse(e) {
  var lock = LockService.getPublicLock();
  lock.waitLock(30000); 

  try {

    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);


    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; 
    var row = []; 

    for (i in headers){
      if (headers[i] == "Timestamp"){ 
        row.push(new Date());
      } else { 
        row.push(e.parameter[headers[i]]);
      }
    }

    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){

    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { 
    lock.releaseLock();
  }
}

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

Please help me.

Simon H
  • 2,495
  • 4
  • 30
  • 38
Stacy Thompson
  • 688
  • 1
  • 10
  • 26
  • From this [SO question](http://stackoverflow.com/questions/18804649) it seems that this error is caused by the wrong implementation of a function. I'm not sure why the appscript give this kind of error instead of proper error message. So to help you to determine the error in your code, try to debug it by using [Execution transcript](https://developers.google.com/apps-script/troubleshooting#debugging) everytime you run the code. Try also to check the [debugger and breakpoints](https://developers.google.com/apps-script/troubleshooting#using_the_debugger_and_breakpoints). – KENdi Feb 23 '17 at 14:42

0 Answers0