简体   繁体   中英

Why is google apps script changing my user property to some random code every time I save it?

I am creating an editor add-on for google sheets and I'm currently stumped on why my user property ( MY_WEBHOOK ) is being changed every time I try to save it with setProperty to this code:

function(){var L=h(fb(arguments));if(b&32)Vd(function(){a.apply(g||this,L)});else return m(a.apply(g||this,L))}

Here is the code I am using:

code.gs :

function saveSettings(url) {
  PropertiesService.getUserProperties().setProperty('MY_WEBHOOK', url);
  SpreadsheetApp.getUi().alert("Saved")
}

function getSettings() {
  return PropertiesService.getUserProperties().getProperty('MY_WEBHOOK');
}

In my html file:

<body onload="get()">

 <form>
    
    <label>What is the URL?</label>
    <input id="webhook-url" type="url" autofocus required placeholder="Webhook Here">
    <br><br>
    <input type="button" value="Save" onclick="save()">
    
    <script>
    function save() {
       var url = document.getElementById('webhook-url').value
       google.script.run.saveSettings(url)
       alert(url)
       alert(google.script.run.getSettings)
       google.script.host.close()
    }
    
    function get() {
       var settings = google.script.run.getSettings
       document.getElementById('webhook-url').value = settings;
    }
    </script>
    
  </form>
    
</body>

Modification points:

I think that there are 2 modification points in your script.

  1. About google.script.run.getSettings , in this case, the function of getSettings is not run. Please add () like google.script.run.getSettings() . By this, the function is run.
    • I think that this is the reason of your issue.
  2. About alert(google.script.run.getSettings) and var settings = google.script.run.getSettings , google.script.run returns no value. So in this case, withSuccessHandler is used.
  3. google.script.run is run with the asynchronous process.

When above points are reflected to your script, it becomes as follows.

Modified script:

Please modify your Javascript as follows.

function save() {
  var url = document.getElementById('webhook-url').value
  google.script.run.withSuccessHandler(() => {
    google.script.run.withSuccessHandler(e => {
      alert(e);
      google.script.host.close();
    }).getSettings();
  }).saveSettings(url);
}

function get() {
  google.script.run.withSuccessHandler(settings => {
    document.getElementById('webhook-url').value = settings;
  }).getSettings();
}
  • When above script is used, at first, the value is retrieved from getSettings by get() , and when the value is inputted and click button, the value is put by saveSettings() and the current value is retrieved by getSettings() and alert() is run, and then, google.script.host.close() is run.

Note:

  • This is a simple modification. So please modify it for your actual situation.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM