简体   繁体   English

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

[英]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:我正在为谷歌表格创建一个编辑器插件,我目前很难理解为什么每次我尝试使用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))}

Here is the code I am using:这是我正在使用的代码:

code.gs : 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:在我的 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>

Modification points:修改点:

I think that there are 2 modification points in your script.我认为您的脚本中有 2 个修改点。

  1. About google.script.run.getSettings , in this case, the function of getSettings is not run.关于google.script.run.getSettings ,在这种情况下, getSettings的 function 没有运行。 Please add () like google.script.run.getSettings() .请添加()google.script.run.getSettings() By this, the function is run.通过这个,function 运行。
    • 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.关于alert(google.script.run.getSettings)var settings = google.script.run.getSettingsgoogle.script.run不返回任何值。 So in this case, withSuccessHandler is used.所以在这种情况下,使用withSuccessHandler
  3. google.script.run is run with the asynchronous process. google.script.run与异步进程一起运行。

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

Please modify your Javascript as follows.请按如下方式修改您的 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();
}
  • 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.使用上述脚本时,首先通过get()getSettings中获取值,当输入值并单击按钮时,通过saveSettings()输入值,通过getSettings()alert()获取当前值alert()运行,然后google.script.host.close()运行。

Note:笔记:

  • This is a simple modification.这是一个简单的修改。 So please modify it for your actual situation.所以请根据您的实际情况进行修改。

Reference:参考:

暂无
暂无

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

相关问题 Google Apps脚本正在将我的数组更改为字符串。 为什么? - Google Apps Script is changing my Array into a string. Why? Google Apps 脚本 - 如何加快我的代码速度? - Google Apps Script - How can i speed up my code? 如何在我的 Google Forms 中添加我的 Google Apps 脚本签名代码 - How do I add my Google Apps Script signature code within my Google Forms Google Apps 脚本 - 在动态更改的标签编号上运行相同的代码 - Google Apps Script - Run Same Code on Dynamically Changing Tab Numbers 如何更改我的Google Apps脚本代码,以允许将多个文件上传到Google云端硬盘? - How do I alter my Google Apps Script code to allow multiple files to be uploaded to Google Drive? 为什么我收到 TypeError:在 Google 表格上运行应用程序脚本时无法读取未定义的属性“1”? - Why I'm getting TypeError: Cannot read property '1' of undefined while running an apps script on Google Sheets? 使用Google Apps脚本的工作流程-我需要在第一步中向用户发送电子邮件 - Workflow using Google Apps Script - I need to email the user in my first step 如何根据用户在谷歌应用脚本中提供的偏移量获取时间? - How to get time according to the offset provided by the user in google apps script? Google +1,每次单击都会中断。 使用Google的代码。 怎么破了 - Google +1, breaks every time I click it. Using Google's code. Why's it breaking 为什么这个谷歌应用程序脚本只有在我硬编码范围时才能工作? - Why will this Google apps script only work if I hard-code the range?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM