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



我正在为google sheets创建一个编辑器附加组件,我目前被为什么我的用户属性(MY_WEBHOOK)每次我试图用setProperty保存它时都被更改为以下代码:

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>

修改点:

我认为你的脚本有两个修改点。

  1. 关于google.script.run.getSettings,在此例中,没有运行getSettings的功能。请像google.script.run.getSettings()一样添加()。这样,函数就运行了。
    • 我认为这就是你的问题的原因。
  2. 关于alert(google.script.run.getSettings)var settings = google.script.run.getSettings,google.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(),然后运行google.script.host.close()

注意:

  • 这是一个简单的修改。因此请根据您的实际情况进行修改。
  • 参考:

类google.script.run

最新更新