Search code examples
google-apps-scriptweb-storage

Google Script PropertiesService storing data in unreadable format


I'm trying to use PropertiesService to store data so that each user's settings appear next time they load the app. I'm also using these properties to build and run a trigger. The problem is that when I try to call the userProperties from functions following the one where they were set, the array shows up as Ljava.lang.Object instead of the actual values. I've tried some things with JSON, but to no avail.

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

Execution transcript

[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]

Solution

  • When you set a property the value has to be a string. If you are saving an Object make sure you convert it to JSON with JSON.stringify() first. When you later get the value parse JSON back to an object at that time.

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

    My test shows the values of:

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