将持久性HTML表单数据存储在Google Script中

时间:2015-04-27 22:04:35

标签: google-apps-script html-form

我编写了一个Google Script网络应用,用于将选定的Gmail标签备份到用户定义的Google云端硬盘位置。 Forml.html以表格格式显示数据。在每行生命索引(这是复选框),标签和驱动器目标。

我们希望每行中的数据从一个页面加载持续到下一个页面,以便选定的行保持选中状态。在我弄清楚如何使数据保持不变之后,我可以构建一个触发器,使应用程序按计划运行并备份所选标签。有关如何最好地实现随时间存储HTML表单数据的效果的任何想法?我正在考虑使用属性服务,但我不知道如何更改初始的doGet以在空白表单和具有预先存在的数据的表单之间进行选择。

Form.html

<div id="formDiv">
    <form id="myForm" target="_self">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th scope="col">
                        <h3 span style="font-family:arial,helvetica,sans-serif;">Select</span></th>
                    <th scope="col">
                        <h3 span style="font-family:arial,helvetica,sans-serif;">Label</span></th>
                    <th scope="col">
                        <h3 span style="font-family:arial,helvetica,sans-serif;">Drive target</span></th>
                </tr>
            </thead>
            <tbody>
                <? var label = GmailApp.getUserLabels();
                   for (i = 0; i < label.length; i++) { ?>    
                <tr>
                    <td style="text-align: center;">
                        <span style="font-family:arial,helvetica,sans-serif;"><input id="index" type="checkbox" name="index" value="<?= [i]; ?>"  /></span></td>
                    <td>
                        <span style="font-family:arial,helvetica,sans-serif;"><input id="label" type="text" style="border:none" size="60" name="label" value="<?= label[i].getName(); ?>" readonly /></span></td>
                    <td>
                        <span style="font-family:arial,helvetica,sans-serif;">Gmail Backup/<input id="target" type="text" size="60" maxlength="128" name="target" value="<?= label[i].getName(); ?>" /></span></td>
                </tr>
                <? } ?>
            </tbody>
        </table>
        <p><input type="submit" value="Test" onclick="google.script.run.gmailBackup(this.parentNode.parentNode)" />   
        </p>
    </form>
</div>

Code.gs

    function doGet() {
      var template = HtmlService.createTemplateFromFile("Form")
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.NATIVE);
      return template;
    }

    function gmailBackup(template) {
      // Set HTML form variables
      var indices = template.index;
      var labels = template.label;
      var targets = template.target;
      for (i = 0; i < indices.length; i++) {
        var index = indices[i].toString();
        var label = labels[indices[i]].toString();
        var target = "Gmail Backup/" + targets[indices[i]].toString();    
    ...
    }

1 个答案:

答案 0 :(得分:0)

我最终使用PropertiesService来基于每个用户存储HTML表单属性。这看起来效果会很好(特别是一旦我发现每个Where does Google Script PropertiesService store data?存储数据的确切位置)。

来自Form.html

<input onclick="google.script.run.setProperties(this.parentNode.parentNode);" type="submit" value="Save" >

来自Code.gs

function setProperties(form) {
  var filledForm = PropertiesService.getUserProperties().setProperty("FORM", form);
}