2015-04-29 43 views
0

我試圖使用PropertiesService來存儲數據,以便下次加載應用程序時出現每個用戶的設置。我也使用這些屬性來構建和運行觸發器。問題是,當我嘗試從函數中調用userProperties後,數組顯示爲Ljava.lang.Object而不是實際值。我用JSON嘗試了一些東西,但無濟於事。Google Script PropertiesService以不可讀的格式存儲數據

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 ([email protected]#$%&*), Test 02 - A ([email protected]#$%&*)/Test 02 - B ([email protected]#$%&*), Test 02 - A ([email protected]#$%&*)/Test 02 - B ([email protected]#$%&*)/Test 02 - C ([email protected]#$%&*)], 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 ([email protected]#$%&*), Test 02 - A ([email protected]#$%&*)/Test 02 - B ([email protected]#$%&*), Test 02 - A ([email protected]#$%&*)/Test 02 - B ([email protected]#$%&*)/Test 02 - C ([email protected]#$%&*)]}]) [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] 

回答

1

當您設置一個PROPERT y值必須是一個字符串。如果您要保存對象,請確保先使用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"]} 
+0

那太好了,謝謝你。我以爲我試過沒有成功,但第二次看,它工作正常。 – TsunamiSteve

相關問題