Saturday, February 20, 2021
Home Blogging How to Share User Properties between Google Apps Script Projects

How to Share User Properties between Google Apps Script Projects

How to Share User Properties between Google Apps Script Projects

The Properties Service of Google Apps Script is utilized by designers to store application setup and client explicit settings. The properties information is checked to a particular client, or a particular undertaking, and can’t be divided among various ventures.

The Email Form Notifications add-on likewise utilizes the Properties Service to store decides that are characterized by the client. The standards made by User An are not available to User B.

Notwithstanding, in some particular cases, we might need to offer admittance to our store information to another client so they may expand upon the current setup as opposed to building everything without any preparation.

The new import trade choice permits the client to send out properties information as a plain book document that can be brought into the property store of another client.

Access the Property Store

On the worker side (Google Script), we characterize two techniques – one for sending out information as a JSON record and the other strategy for bringing in information from the property store of another client 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

For exporting data, the HTML file contains a simple download button that connects to the server, fetches the data and allows the user to save this data as a text file on their computer.

<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

For importing data into the property store, the user can upload a text (JSON) file that contains data as key-value pairs. These files are easily readable in any text editor and you can also add define new properties by adding new 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>

The File Reader API of JavaScript is utilized to peruse the substance of the chose text document. The onload occasion gets terminated when the document has been effectively perused in memory.

The readAsText technique for File Reader will peruse the document as a string yet you may likewise utilize the readAsDataURL strategy ought to be wish to transfer record in base64 encoded design that can be decoded on the worker.

namanmahajanhttp://artistogram.in
I am a Multimedia artist, I am born and based on Dewas (M.P), I am working as a freelancer with various brands and different country. My expertise are Graphics designing, motion graphics, 2d/3d animation, advertising videos, cartoons designing, all types of packaging and branding designing, digital marketing, SEO, design website and app Developments etc...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular