如何使用侧边栏表单的输入并将其存储为Google Apps脚本中的全局变量?



我正试图使用我在Google Sheets的侧边栏中键入的内容作为全局变量(以便我可以在脚本稍后的各种不同函数中使用该值)。

使用下面的.gs.html代码,我可以将输入传递给另一个函数(这是saveValue函数),但我不知道如何永久存储该值,或者至少直到我使用侧边栏放置另一个值。

var ui = SpreadsheetApp.getUi();
function sidebar() {
var temp = HtmlService.createHtmlOutputFromFile('index')
temp.setTitle('Title');
var html = temp.evaluate();
ui.showSidebar(html);
}
function saveValue(inValue) {
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p> This is a test</p>
<br>
<input id='input-one' type='text'>
<label form='input-one'>Input</label>
<br>
<br>
<button id='save-values'>Save Values</button>
<br>
<br>
<input type="button" value="Close" onclick="google.script.host.close()" />
<script>
document.getElementById('save-values').addEventListener('click',function(){
var inValue = document.getElementById('input-one').value();
google.script.run.withSuccessHandler().saveValue(inValue);
});
</script>
</body>
</html>

属性配额

const p = PropertiesService.getScriptProperties();
p.setProperty('key', inValue);
/* load with
p.getProperty('key');
*/

如果你不想覆盖和存储所有的值,你可以考虑使用:

数组

let array = p.getProperty('key');
if (!array) { array = []; }
else { array = JSON.parse(array); }
array.push(inValue);
p.setProperty('key', JSON.stringify(array));

增量键

const count = p.getProperty('count') || 0;
p.setProperty(`key${count + 1}`, inValue);

但我建议在这种情况下存储在一个表格。

最新更新