Home > Mobile >  Function not executing when called second time
Function not executing when called second time

Time:09-17

I am working on a project where I take inputs from a html form and submit them inside of google sheets. Its working fine with no errors. So I wanted to add a function where the user can resubmit the data incase of server-side error. The issue is when I am calling the same function which submits the data(working) it doesnt seem to respond. I am sending the same array of data yet it doesnt return any server response. If I refresh the entire page it works again, its just that it wont let me run the same function twice in one session. Here are the codes I used:

Html:

google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).addRecord(rows);

GAS:

function doGet(request) {
return HtmlService.createHtmlOutputFromFile('index');
 }
//division,block,ward,booth,committee,designation,name,phone,email
function addRecord(rows) {
  var url = 'https://docs.google.com/spreadsheets/d/-myProjectID-/edit#gid=0';
  var ss = SpreadsheetApp.openByUrl(url);
  var webAppSheet = ss.getSheetByName("FORM DATA");
  var row = rows.length;
  var column = rows[0].length;

 //LockService.getScriptLock.waitLock(10000);
 // var lock = LockService.getScriptLock();
 // lock.waitLock(10000);    //prevent creating 2 sessions simultaneously

 try {
  webAppSheet.getRange(webAppSheet.getLastRow()   1, 1, row, column).setValues(rows);
  // lock.releaseLock();
  return "Ok"
  } catch (ex) {
    lock.releaseLock();
    return "Error"
 }
}

function getScriptURL() {
  return ScriptApp.getService().getUrl();
 }

to retry i run the same function "addRecord()" and pass the same array"rows[]". Can anyone point out where I am doing wrong or is it some special attribute in GAS that prevents me from doing otherwise.

CodePudding user response:

I'd try something like this:

gs:

function addRecord(rows) {
  const ss = SpreadsheetApp.openById(myProjectId);
  const sh = ss.getSheetByName("FORM DATA");
  sh.getRange(sh.getLastRow()   1, 1, rows.length, rows[0].length).setValues(rows);
  return "Ok"
}

js:

function dontknowname() {
  //disable send button here
  google.script.run
    .withSuccessHandler(() => 'enable send button here')
    .addRecords(rows);
}
  • Related