我是Google电子表格的管理员。我一周几次和其他管理员必须在电子表格中添加新用户,所以我认为如果我们可以使用自定义HTML对话框这样做会更容易。
我正在显示对话框:
function AddRow()
{
var html = HtmlService.createHtmlOutputFromFile('AddRow')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(html, 'Add Row')
}
文件AddRow.html是一个带有文本框,选择元素等的简单HTML表单。
现在我有办法访问电子表格,添加一个新行,其中包含用户输入的值吗?
在HTML文件AddRow.html中,我尝试了以下
<script>
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('2015');
sheet.insertRows(2, 1);
</script>
但是这不会插入一行。如果我将HTML文件的代码移动到* .gs文件中它确实有用,那么它必须是对HTML文件的限制吗?
答案 0 :(得分:0)
提供的HTML文件中的脚本代码是javascript,而不是Google Apps脚本 - 此问题的主要区别在于服务器端Google Apps脚本可以访问为Google服务提供的全套API,而客户端则javascript没有。
一种选择是使用google.script.run
Client-side API来调用您已在服务器端工作的功能(在编辑器中)。有关详细信息,请参阅guide to communicating with server functions in HTML service。
function insertRows(rowIndex, numRows) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('2015');
sheet.insertRows(rowIndex, numRows);
return true; // Need to have a return value for google.script.run to work
// To return a failure to the client side, throw an error.
}
客户端脚本:
...
// Call insertRows on server side.
google.script.run
.withSuccessHandler( hooray ) // See function hooray below
.insertRows(2, 1);
...
/**
* This function will receive a call-back if the google.script.run
* call returns success.
*/
function hooray( result ) {
alert( "Received result <" + result + ">" );
}