Share User Properties between Google Apps Script Projects

Hello, Guys welcome back, learn How to Share User Properties between Google Apps Script Projects. Developers use Google Apps Script’s Properties Service to store app configuration and user-specific settings. The properties data is restricted to a single user or project and cannot be shared across projects.

Share User Properties between Google Apps Script Projects

The Email Form Notifications add-on additionally makes use of the Properties Service to hold user-defined rules. User B does not have access to the rules defined by User A.

However, in other circumstances, we may want to grant another user access to our store data so that they can build on the existing configuration rather than starting from zero.

The new import-export option allows users to export property data as a plain text file that can be imported into another user’s property store.

Access the Property Store

On the server (Google Script), we define two methods: one for exporting data as a JSON file and another for importing data from another user’s property store into our own.

/* Choose DocProperties for editor add-ons */
const getStore = () => {
  return PropertiesService.getUserProperties();
};

/* Export user settings */
const exportUserData = () => {
  const data = getStore().getProperties();
  return JSON.stringify(data);
};

/* Import user settings */
const importUserData = (data) => {
  const json = JSON.parse(data);
  getStore().setProperties(json);
  return 'OK';
};

Export User Properties as a Text File

The HTML file provides a basic download button for exporting data, which connects to the server, retrieves the data, and allows the user to save the data as a text file on their machine.

<p>Export Data</p>
<button onclick="downloadFile();return false;" href="#">Export</button>

<script>
  function downloadFile() {
    google.script
      .withSuccessHandler(function (data) {
        var a = document.createElement('a');
        var blob = new Blob([data], {
          type: 'text/plain',
        });
        var url = URL.createObjectURL(blob);
        a.setAttribute('href', url);
        a.setAttribute('download', 'file.txt');
        a.click();
      })
      .exportUserData();
  }
</script>

Import User Properties from a Text File

The user can import data into the property store by uploading a text (JSON) file containing data as key-value pairs. These files are easily accessible in any text editor, and new attributes can be defined simply adding additional keys to the JSON file.

<p>Import data</p>
<input type="file" id="file" accept="text/plain" />

<script>
  document.getElementById('file').addEventListener(
    'change',
    function (event) {
      var file = event.target.files[0];
      if (file.type !== 'text/plain') {
        window.alert('Unsupported file');
        return;
      }
      var reader = new FileReader();
      reader.onload = function (e) {
        google.script.run
          .withSuccessHandler(function (success) {
            window.alert(success);
          })
          .withFailureHandler(function (err) {
            window.alert(err);
          })
          .importUserData(e.target.result);
      };
      reader.readAsText(file);
    },
    false
  );
</script>

JavaScript’s File Reader API is used to read the contents of the specified text file. When the file is successfully read into memory, the onload event is triggered.

The readAsText function of File Reader will read the file as a string, but if you want to upload a file in base64 encoded format that can be decoded on the server, you can use the readAsDataURL method.

Leave a Comment