NobleUplift
NobleUplift

Reputation: 6005

Is PropertiesService.getUserProperties() of Google Apps Script deprecated?

I am trying to use this S3 library for Google Apps Script. When I go to testSupport.gs and I try to run setTestEnv, it fails with SyntaxError: Unexpected token: u (line 28, file "testSupport").

This makes sense, since I haven't set any User Properties yet, let me— oh, wait, that's not right...

No User properties tab

Even if I clone this script, I still do not have a "User properties" tab. Is there something I'm missing here?

Upvotes: 0

Views: 807

Answers (1)

Aerials
Aerials

Reputation: 4419

The PropertiesService.getUserProperties() method is not deprecated.

In fact, the error message is correct. There is an important difference between JavaScript and JSON.

In a JSON object property names MUST be double-quote strings.

Take a look at the following code for testSupport.gs:

/** @define {string} */
var TEST_ENV_NAME_ = "S3LibraryTestEnv";
// todo: consider splitting into a generic testing lib?

// Format the TestEnv Object with property names in double quotes.
var S3LibraryTestEnv = '{"awsAccessKeyId":"theId","awsSecretKey":"theKey"}';

// Run test() to check it out
function test(){
  setTestEnv(S3LibraryTestEnv);
}

/**
* sets a testing Env, that's accessible from test and demo functios; and persistent for user
* 
* @param {Object} env the environment object to set for testing (should have awsAccessKeyId, awsSecretKey as properties)
* @return {void}
*/
function setTestEnv(env) {
  var userProperties = PropertiesService.getUserProperties();

  // env is a JSON format string eg. ("Property names must be double-quoted strings")
  // No need to use JSON.stringify()
  userProperties.setProperty(TEST_ENV_NAME_, env);  
}

/**
* gets Test Env for the library, optionally skipping validing that all req constants are defined in that env
*
* @param {boolean}  
* @return {Object} key-value for the environment
*/
function getTestEnv(skipEnvValidation) {
  userProperties = PropertiesService.getUserProperties();
  var env = JSON.parse(userProperties.getProperty(TEST_ENV_NAME_));
  // Logger.log(env);
  if (!skipEnvValidation) {
    var requiredKeys = ["awsAccessKeyId", "awsSecretKey"];

    if (env == null) {
      throw "Must set environment in UserProperties (see setTestEnvFromUI)"; 
    }
    for (var i=0; i < requiredKeys.length; i++) {
      if (typeof env[requiredKeys[i]] == 'undefined') {
        throw "Test Environment is missing required property '" + requiredKeys[i] + "'.  Define it object passed to setTestEnv().";
      }
    }
  }
  return env;
}

Also fixed some typo's here and there.

Upvotes: 1

Related Questions