【发布时间】:2023-04-09 06:36:01
【问题描述】:
鉴于目前的情况,我希望每个人都安全健康。
我有一个关于使用 google 应用程序脚本的项目的问题。我有一个网络应用程序,我已经能够使用链接等找出 doGet() 的路由。
//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 中成功。有没有办法检查?我可以通过事件对象获取所有参数,但不能获取状态。
<form id="myForm" onsubmit="handleNewAccountFormSubmit(this);" method="post" action="<?= ScriptApp.getService().getUrl(); ?>">
我也一直在尝试使用包含的 .withFailureHandler() 来处理它,但我不确定如何让它触发,或者是否可以从我的 .GS 回调函数 我也尝试在 FormSubmit 函数之外使用 onFail() 函数。
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();
}
}
我基本上想显示该功能是否成功运行以获得用户体验,但不确定最佳实践或如何或即使可能(我相信它是可能的!)
我期待任何想法和更正,如果有不清楚的地方,我会尽力提供更多信息。
再次感谢。
【问题讨论】:
-
这个:
<a href="<?= ScriptApp.getService().getUrl(); ?>?v=newAccountForm">Add New Brand</a>是一个 get 调用,将加载一个新的 html。那么,有什么问题呢? -
@TheMaster 是的,那部分工作正常。我试图提供额外的信息。当用户提交表单时,我有两个选择。表单转到空白页,或者我可以点击 doPost 函数。选项 1 - 我想找到一种方法来传递参数,或者如何在 doPost 中判断函数是否成功。从那里我可以呈现错误页面或成功页面。选项 2 - 使用失败或成功处理程序运行函数以触发另一个函数?(不确定是否可能)。即使我删除了 POST 方法,onFail() 也不起作用。希望清除它。谢谢
-
或者选项 3 是使用 .getUrl(); 重定向回 doGet(); + ?v=error in the withHandlers 但我不确定如何在没有链接的情况下执行此操作。这可能在脚本中吗?我想如果是的话,我可以创建成功和失败的路线,但我可能无法通过参数
-
Logger客户端不可用。
标签: javascript forms google-apps-script web-applications routes