Response and Error handling in Google Apps Scripts. doPost? withFailureHandler()?

风流意气都作罢 提交于 2020-04-15 15:43:43

问题


I hope everyone is safe and healthy given the current situation.

I have a question in regards to a project with google apps script. I have a web app and I have been able to figure out routing with doGet() using links etc.

//global variables
const sheetId = "foo";
const Route = {};
Route.path = function(route, callback){
  Route[route] = callback;
}

function doGet(e){

  Route.path("newAccountForm",loadNewForm);
  Route.path("updateBrandForm", loadUpdateForm);

  if(Route[e.parameters.v]) {
       return Route[e.parameters.v](); 
  } else {
    return render("home") 
  }
};

function loadNewForm() {

 const sheetActive = SpreadsheetApp.openById(sheetId);
 const mySheet = sheetActive.getSheetByName("Sheet1");

  const title = "title";
  const index = "index";

  return render("addNewAccount",{title: title, index: index});  

}

function loadUpdateForm () {
  const sheetActive = SpreadsheetApp.openById(sheetId);
  const mySheet = sheetActive.getSheetByName("Sheet1");


  return render("updateBrand");

}

function render(file,argsObject) {
  const tmp = HtmlService.createTemplateFromFile(file);
  if(argsObject) {
    const keys = Object.keys(argsObject);
    keys.forEach(function(key){
      tmp[key] = argsObject[key];
    })   
  }  // END IF  
  return tmp.evaluate();  
}

The links..

    <a href="<?= ScriptApp.getService().getUrl(); ?>?v=newAccountForm">Add New Brand</a> 
    <a href="<?= ScriptApp.getService().getUrl(); ?>?v=updateBrandForm">Update Exisiting Brand</a> 
    <a href="<?= ScriptApp.getService().getUrl(); ?>?v=reports">Analytics / Reports</a> 

Now I am a bit stuck on handling responses and errors. I have tried using doPost() which works to render a new HTML page. My problem is I am unsure how to tell if the request was successful in the doPost. Is there a way to check that? I can get all the parameters through the event object but not a status.

<form id="myForm" onsubmit="handleNewAccountFormSubmit(this);"  method="post" action="<?= ScriptApp.getService().getUrl(); ?>">

I have also been trying to handle it with the included .withFailureHandler() but am unsure how to get it to fire or if it is possible to call back a function from my .GS I have tried also having the onFail() function outside the FormSubmit function.

function handleNewAccountFormSubmit(formObject) {
google.script.run.withFailureHandler(onFail).withSuccessHandler().processNewAccountForm(formObject);
  function onFail(error) {
  Logger.log(error)
  console.log(error)
  return google.script.run.onError();
}
}

I basically want to show if the function ran successfully for user experience but am unsure of best practise or how or even if it is possible(I am sure it is!)

I look forward to any ideas, corrections, and if something is unclear I will do my best to provide more info.

Thanks again.


回答1:


Use success or failure handlers to alert the user:

function handleNewAccountFormSubmit(formObject) {    
    alert("Please wait..!")  
    google.script.run
        .withFailureHandler(e => {
            console.error(e.message);
            alert("Unexpected error! Contact support!")
        })
        .withSuccessHandler(e => alert("Form submitted successfully!"))
        .processNewAccountForm(formObject);
}


来源:https://stackoverflow.com/questions/60771819/response-and-error-handling-in-google-apps-scripts-dopost-withfailurehandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!