问题描述
我希望每个人在目前的情况下都能安全健康。
I hope everyone is safe and healthy given the current situation.
我对使用Google Apps脚本的项目有疑问。我有一个Web应用程序,并且能够使用链接等方法找出doGet()的路由。
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();
}
链接。
<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>
现在我在处理响应和错误时有点卡住了。我尝试使用doPost()来呈现新的HTML页面。我的问题是我不确定如何确定doPost中的请求是否成功。有没有办法检查?我可以通过事件对象获取所有参数,但不能获取状态。
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(); ?>">
我也一直在尝试使用包含的.withFailureHandler()处理它,但不确定如何获取它会触发,或者是否有可能从我的.GS
回调函数。我也尝试过在FormSubmit函数之外使用onFail()函数。
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 .GSI 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.
再次感谢。
推荐答案
使用成功或失败处理程序来提醒用户:
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);
}
这篇关于Google Apps脚本中的响应和错误处理。 doPost? withFailureHandler()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!