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>
I think that there are 2 modification points in your script.
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.
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.google.script.run
is run with the asynchronous process. When above points are reflected to your script, it becomes as follows.
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();
}
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.
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.