【发布时间】:2020-05-03 23:46:18
【问题描述】:
我有一个带有自定义边栏的 Google 电子表格。边栏仅包含带有 <textarea> 的表单。
<textarea> 使用脚本从表格单元格中获取值。
我想通过onchange 触发器将新值从侧边栏发送到同一单元格,或者如果用户未更改旧值,则将旧值保留在单元格中。
你可以在下面看到我所拥有的。不幸的是,它不起作用。不起作用意味着侧边栏从单元格中获取价值,但在更改后不发送新的。我不明白为什么我错了以及如何解决?
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Change.html')
.setTitle('Cooking method')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function addNewMethod(form_data)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Method');
var cookingMethod = form_data.method;
sheet.getRange('A2').setValue(cookingMethod);
}
function getDefaultMethod() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Method');
const currentMethod = sheet.getRange('A2').getValue();
return currentMethod
}
<!doctype html>
<html lang="en">
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<meta charset="utf-8">
<title>change</title>
<style>
body{
background-color: #3366CC;
}
textarea {
margin: 10px
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function()
{
google.script.run
.withSuccessHandler(updateMethod)
.getDefaultMethod();
});
function updateMethod(method_val)
{
document.getElementById('method').innerHTML=method_val;
}
</script>
</head>
<body>
<form id="mysidebar">
<div class="block form-group">
<label for="method"></label>
<textarea id="method" name="method" rows="30" cols="45">
</textarea>
</div>
</form>
<div></div>
<script>
$("method").change(function () {
google.script.run.addNewMethod(this);
}).change();
</script>
</body>
</html>
【问题讨论】:
-
1.可以问一下
it does not work的详细信息吗? 2.your previous question你会怎么做? -
不幸的是,它在库中不起作用(我稍后会修复它)。但适用于原生电子表格。现在我明白了我不需要按钮来提交表单。当用户更改侧边栏中的文本时,获得新值会更好。每个电子表格都将包含
Method表以应用此边栏。 -
感谢您的回复。关于
(I'll fix it later),我能理解。关于你的这个问题,在这种情况下,GAS脚本和HTML放在容器绑定脚本的一个GAS项目中而不使用库? -
很抱歉误解了第一条评论。 1. 这个问题的
Does not work表示<textarea>从单元格中获取值,但在我更改侧边栏中的文本时不发送新值。 2. 对于我之前的问题 - 不幸的是,它在库中不起作用(我稍后会修复它)。 -
关于容器绑定脚本。我需要制作太多模板电子表格副本。每个副本也将复制容器绑定脚本。因此,如果我需要修改我的主脚本,我将需要更新每个副本。 1000 多个电子表格会非常复杂
标签: javascript html google-apps-script google-sheets