问题
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