繁体   English   中英

为什么每次保存时谷歌应用程序脚本都会将我的用户属性更改为一些随机代码?

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

我正在为谷歌表格创建一个编辑器插件,我目前很难理解为什么每次我尝试使用setProperty将其保存到此代码时都会更改我的用户属性( MY_WEBHOOK ):

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

这是我正在使用的代码:

code.gs

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

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

在我的 html 文件中:

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

修改点:

我认为您的脚本中有 2 个修改点。

  1. 关于google.script.run.getSettings ,在这种情况下, getSettings的 function 没有运行。 请添加()google.script.run.getSettings() 通过这个,function 运行。
    • 我认为这是您的问题的原因。
  2. 关于alert(google.script.run.getSettings)var settings = google.script.run.getSettingsgoogle.script.run不返回任何值。 所以在这种情况下,使用withSuccessHandler
  3. google.script.run与异步进程一起运行。

当以上几点反映到您的脚本时,它变成如下。

修改后的脚本:

请按如下方式修改您的 Javascript。

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();
}
  • 使用上述脚本时,首先通过get()getSettings中获取值,当输入值并单击按钮时,通过saveSettings()输入值,通过getSettings()alert()获取当前值alert()运行,然后google.script.host.close()运行。

笔记:

  • 这是一个简单的修改。 所以请根据您的实际情况进行修改。

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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