Google 脚本 PropertiesService 以不可读的格式存储数据

Google Script PropertiesService storing data in unreadable format

我正在尝试使用 PropertiesService 来存储数据,以便每个用户的设置在他们下次加载应用程序时显示。我还使用这些属性来构建和 运行 触发器。问题是,当我尝试从设置它们的函数后面的函数调用 userProperties 时,数组显示为 Ljava.lang.Object 而不是实际值。我用 JSON 尝试了一些方法,但无济于事。

Form.html

<?!= include('Style'); ?>

<div id="formDiv">
    <form id="myForm">
        <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.setProperties(this.parentNode.parentNode)" />   
        <input type="submit" value="Save" onclick="google.script.run.createTrigger(); google.script.run.thanks()" />   
        </p>
    </form>
</div>

<?!= include('JavaScript'); ?>

Code.gs

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

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

function propTest() {
  var userProperties = PropertiesService.getUserProperties();
  var filledForm = userProperties.getProperty("FORM");
  Logger.log(filledForm);
}

执行记录

[15-04-29 09:54:09:912 MDT] Starting execution
[15-04-29 09:54:09:932 MDT] PropertiesService.getUserProperties() [0 seconds]
[15-04-29 09:54:10:032 MDT] (class).setProperty([FORM, {index=[3, 4], target=[Test 01-A, Test 01-A/Test 01-B, Test 01-A/Test 01-B/Test 01-C, Test 01-A/Test 01-B/Test 01-C/Test 01-D, Test 01-A/Test 01-B/Test 01-C/Test 01-D/Test 01-E, Test 02 - A (+_!@#$%&*), Test 02 - A (+_!@#$%&*)/Test 02 - B (+_!@#$%&*), Test 02 - A (+_!@#$%&*)/Test 02 - B (+_!@#$%&*)/Test 02 - C (+_!@#$%&*)], label=[Test 01-A, Test 01-A/Test 01-B, Test 01-A/Test 01-B/Test 01-C, Test 01-A/Test 01-B/Test 01-C/Test 01-D, Test 01-A/Test 01-B/Test 01-C/Test 01-D/Test 01-E, Test 02 - A (+_!@#$%&*), Test 02 - A (+_!@#$%&*)/Test 02 - B (+_!@#$%&*), Test 02 - A (+_!@#$%&*)/Test 02 - B (+_!@#$%&*)/Test 02 - C (+_!@#$%&*)]}]) [0.098 seconds]
[15-04-29 09:54:10:032 MDT] PropertiesService.getUserProperties() [0 seconds]
[15-04-29 09:54:10:065 MDT] (class).getProperty([FORM]) [0.031 seconds]
[15-04-29 09:54:10:065 MDT] Logger.log([{index=[Ljava.lang.Object;@6cb426d, target=[Ljava.lang.Object;@a760597, label=[Ljava.lang.Object;@4a6901de}, []]) [0 seconds]
[15-04-29 09:54:10:066 MDT] Execution succeeded [0.133 seconds total runtime]

当您设置 属性 时,值必须是字符串。如果您要保存对象,请确保先使用 JSON.stringify() 将其转换为 JSON。当您稍后获取值时,解析 JSON 回到当时的对象。

 var formProperties = userProperties.setProperty("FORM", JSON.stringify(form));

我的测试显示以下值:

[15-04-29 13:57:20:225 EDT] {"index":"1","target":["test1","test2"],"label":["test1","test2"]}