我尝试在电子表格上实现侧边栏,以获取用户输入以供我的脚本使用。我无法让它成功调用任何服务器端函数。我将谷歌文档中的一个简单脚本和我读过的几个 stackoverflow 问题放在一起,但我不断收到错误。它能够打印到控制台,但尝试调用时出错日志文本()功能与谷歌脚本运行.
脚本文件:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Extra Functions')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Test')
.setTitle('Testing')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function logInput(text) {
Logger.log(text);
}
HTML 文件(Test.html):
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function onFailure(error) {
var div = document.getElementById('output');
div.innerHTML = "ERROR: " + error.name + ": " + error.message;
}
function logText(){
var txt = document.getElementById("txt_input").value;
console.log(txt);
google.script.run.withFailureHandler(onFailure).logInput(txt);
}
</script>
</head>
<body>
<label for="txt_input">Input Text:</label>
<input type="text" id="txt_input"><br>
<button onclick='logText()'>Send Name</button><br>
<div id="output"></div>
</body>
</html>
我尝试在新的 Apps Script V8 和 Apps Script Legacy 上运行它,但每个错误都略有不同。
Apps 脚本旧版
错误:脚本错误:您无权执行该操作。请请求该项目的所有者授予您访问权限。
应用程序脚本 V8
错误:ScriptError:很抱歉,从存储读取时发生服务器错误。错误代码 PERMISSION_DENIED。
我一直在研究授权,但据我所知,它拥有作为容器绑定脚本所需的所有权限(https://developers.google.com/apps-script/guides/bound)。它有/auth/script.container.uiOAuth 范围,应允许“在 Google 应用程序内的提示和侧边栏中显示和运行第三方网页内容”,以及/auth/电子表格范围。我也是电子表格和脚本项目的所有者。
由于它不作为 Web 应用程序运行,因此不需要部署,也不需要 doGet() 函数。https://developers.google.com/apps-script/guides/html#serve_html_as_a_google_docs_sheets_slides_or_forms_user_interface