【问题标题】:Response and Error handling in Google Apps Scripts. doPost? withFailureHandler()?Google Apps 脚本中的响应和错误处理。邮政?与FailureHandler()?
【发布时间】: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();
}
}

我基本上想显示该功能是否成功运行以获得用户体验,但不确定最佳实践或如何或即使可能(我相信它是可能的!)

我期待任何想法和更正,如果有不清楚的地方,我会尽力提供更多信息。

再次感谢。

【问题讨论】:

  • 这个:&lt;a href="&lt;?= ScriptApp.getService().getUrl(); ?&gt;?v=newAccountForm"&gt;Add New Brand&lt;/a&gt; 是一个 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


【解决方案1】:

使用成功或失败处理程序来提醒用户:

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);
}

【讨论】:

  • 因此,对于此解决方案,第一个警报有效,但只要我在第一个警报上单击“确定”,它就会重定向到空白页面。这与preventDefault()有关吗?我不熟悉,但最近几天读过一些答案。
  • @Noah 是的。您应该preventDefault() 提交事件。检查文档以获取示例表单
  • 我试图弄清楚,但无论我把 preventDefault() 放在哪里,它仍然会进入一个空白屏幕。我错过了什么?
  • 复制此index.html。应在表单加载后立即阻止默认设置。所以,我们使用window.onload
  • 不错的一个!现在我可以通过操作 DOM 等来提醒甚至显示消息。抱歉,我无法理解,哈哈。现在一切都好。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
相关资源
最近更新 更多