我正在尝试使用HTML服务在Google Apps脚本中构建用户界面。我希望我的按钮从“保存”更改为“已保存!”当数据成功保存到用户的脚本属性时。我知道函数saveProperties正在成功执行,因为正在更新用户属性。不知道为什么我正在做的事情不起作用。该按钮仍显示“保存”。
脚本文件
function saveProperties(first, last, email) {
ScriptProperties.setProperty('first', first);
ScriptProperties.setProperty('last', last);
ScriptProperties.setProperty('email', email);
return;
}
html文件
<input type="text" id="first"/>
<input type="text" id="last"/>
<input type="text" id="email"/>
<input id="submitButton" type="submit" value="Save"/>
<script>
function onSubmit(){
var $first = $('#first').val();
var $last = $('#last').val();
var $email = $('#email').val();
google.script.run
.withSuccessHandler(changeButton)
.saveProperties($first, $last, $email);
}
function changeButton(test){
$('#submitButton').attr('value','SAVED!');
}
</script>
答案 0 :(得分:0)
我的测试不包括外部库,例如jQuery,但应该没有区别。
<强> Code.gs 强>
function saveProperties(first, last, email) {
ScriptProperties.setProperty('first', first);
ScriptProperties.setProperty('last', last);
ScriptProperties.setProperty('email', email);
return;
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
<强> JavaScript.html 强>
<script>
function onSubmit(obj) {
var $first = document.getElementById('first').value;
var $last = document.getElementById('last').value;
var $email = document.getElementById('email').value;
google.script.run.withSuccessHandler(changeButton).withUserObject(obj).saveProperties($first, $last, $email);
}
function changeButton(value, obj) {
obj.value = 'SAVED!';
}
</script>
<强>的test.html 强>
<input type='text' id='first'/>
<input type='text' id='last'/>
<input type='text' id='email'/>
<input id='submitButton' name='submitButton' type='button' value='Save' onclick='onSubmit(this)' />
<?!= include('JavaScript'); ?>
更新 - 替代版本:
<强> JavaScript.html 强>
<script>
function onSubmit() {
var $first = document.getElementById('first').value;
var $last = document.getElementById('last').value;
var $email = document.getElementById('email').value;
google.script.run.withSuccessHandler(changeButton).saveProperties($first, $last, $email);
}
function changeButton() {
document.getElementById('submitButton').value = 'SAVED!';
}
</script>
<强>的test.html 强>
<input type='text' id='first'/>
<input type='text' id='last'/>
<input type='text' id='email'/>
<input id='submitButton' name='submitButton' type='button' value='Save' onclick='onSubmit()' />
<?!= include('JavaScript'); ?>