我有一个电子表格,正在编写与Twitter API集成的代码。在我的工作表中,我有两个文件:
我在code.gs上有一些全局变量,即CONSUMER_KEY,CONSUMER_SECRET。我想在用户在文本框中输入值并点击保存按钮时设置这两个变量。
文本框位于侧栏下方。补充工具栏是使用(HtmlService.SandboxMode.IFRAME)
从settingpage.html生成的如果我做错了,或者设置用户输入的全局变量值,请告诉我。
Code.gs
CONSUMER_KEY = "Your key value";
CONSUMER_SECRET = "Your key value";
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('BOT')
.addItem('Setting-config','fnShowSidebar')
.addItem('Start', 'StartCP')
.addSeparator()
.addItem('Stop', 'StopCP')
.addToUi();
}
function fnShowSidebar() {
var html = HtmlService.createHtmlOutputFromFile('settingpage')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Welcome to TwitterBot-RWT!')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
settingpage.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<style>
.centerAlign {
vertical-align: middle;
width:80%;
margin-top:10px;
}
.branding-below {
bottom: 56px;
top: 0;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$("#btn_save").click(function(){
google.script.run.withSuccessHandler(onSuccess).fn_setCustomValue();
});
function onSuccess(){
// Logger.log("Success!");
}
</script>
</head>
<body>
<div class="block form-group">
<label for="city">CONSUMER_KEY</label>
<input id="txt_CONSUMER_KEY" type="text" value="" style="width: 150px;" />
</div>
<div class="block form-group">
<label for="city">CONSUMER_SECRET</label>
<input id="txt_CONSUMER_SECRET" type="text" value="" style="width: 150px;">
</div>
<div class="block">
<button id="btn_save" class="create">Save</button> <button>Cancel</button>
</div>
</body>
</html>
编辑:
HTML
<button id="btn_save" class="create">Save</button>
<button>Cancel</button>
</div>
JS:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(function() {
var buttonEl=document.getElementById("btn_save");
buttonEl.addEventListener("click", myFunction);
function myFunction() {
alert ("Hello World!");
var keyValue=document.getElementById("txt_CONSUMER_KEY").value;
var secretValue=document.getElementById("txt_CONSUMER_SECRET").value;
google.script.run.withSuccessHandler(onSuccess).fn_setCustomValue(keyValue,secretValue);
}
function onSuccess(){
Logger.log("Success!");
}
});
</script>
答案 0 :(得分:0)
当您运行fn_setCustomValue时,您需要将值作为参数传递,您可以使用document.getItemById()来执行此操作。 有点像:
google.script.run.withSuccessHandler(onSuccess).fn_setCustomValue(document.getElementById("txt_CONSUMER_KEY").value, document.getElementById("txt_CONSUMER_SECRET").value);
Code.gs中的fn_setCustomValue()与
一致function fn_setCustomValue(key, secret){
CONSUMER_KEY = key;
CONSUMER_SECRET = secret;
}