Home > database >  How to call another server-side function after data is written to the spreadsheet using withSuccessH
How to call another server-side function after data is written to the spreadsheet using withSuccessH

Time:07-04

The flow is to be basically as follows:

  1. Function addTaskToDb() writes HTML form data to the spreadsheet (e.g. Task 3);
  2. google.script.run.withSuccessHandler(_ => loadClientTasks(selectedClient)).newTask(task); will load updated tasks onto the active HTML page (e.g. Tasks: 1, 2 and 3);
  3. google.script.run.updateFilesWithTask('new', selectedAgency, selectedClient, task); is supposed to get the last task added o the spreadsheet and copy it into other files (should be picking task 3, but it's getting Task 2). It's getting it from there, because there is numbering being applied to that task.
function addTaskToDb() {
  var formElements = document.getElementById("form").elements;
  var postData = [];
  for (var i = 0; i < formElements.length; i  ) {//Converts checkboxes' status to sheets
    if (formElements[i].type != "submit" && formElements[i].type != 'checkbox') {
      postData.push(formElements[i].value);
    } else if (formElements[i].type == 'checkbox' && formElements[i].checked == true) {
      postData.push(formElements[i].checked);
    } else if (formElements[i].type == 'checkbox' && !formElements[i].checked) {
      postData.push('false');
    }
  }
  let timeStamp = new Date();
  timeStamp = timeStamp.toString();
  const agencyPartner = document.getElementById('agencySelect');
  const selectedAgency = agencyPartner.options[agencyPartner.selectedIndex].text;

  const client = document.getElementById('clientSelect');
  const selectedClient = client.options[client.selectedIndex].text;
  
  let dateAssigned = postData[1].toString();
  const item = postData[0];
  const link = postData[2];
  const notes = postData[3];
  const requestApproval = postData[4];

  let task = [];
  task.push(timeStamp, selectedAgency, selectedClient, '', '', dateAssigned, item, link, notes, '', requestApproval, '', '', '')
  google.script.run.withSuccessHandler(_ => loadClientTasks(selectedClient)).newTask(task);
  google.script.run.updateFilesWithTask('new', selectedAgency, selectedClient, task);
  document.getElementById("form").reset();
}

I've tried using Utilities.sleep(3000); within updateFilesWithTask(), but it didn't work.

Thank you!

CodePudding user response:

Have you tried:

google.script.run.withSuccessHandler(_ => { loadClientTasks(selectedClient);
    google.script.run.updateFilesWithTask('new', selectedAgency, selectedClient, task);  
  }
).newTask(task);
  • Related