Google Apps 脚本将表格数据插入文档抛出错误
使用 Google Developer 帮助中心中的代码将表格插入文档效果很好。
// Create a two-dimensional array containing the cell contents.
var cells = [
['Row 1, Cell 1', 'Row 1, Cell 2'],
['Row 2, Cell 1', 'Row 2, Cell 2']
];
// Build a table from the array.
body.appendTable(cells);
但是,我需要做的是将电子表格中的单元格作为表格插入到我的文档中。我尝试按照此处的其他帖子编写此内容:
const body = DocumentApp.getActiveDocument().getBody();
const sheet = SpreadsheetApp.openById("spreadsheetID").getSheetByName("Reports");
var ssCells = sheet.getRange("A2:C5").getValues();
body.appendTable(ssCells);
但它会抛出错误“异常:参数(number[])与 DocumentApp.Body.appendTable 的方法签名不匹配。”我已经记录了 ssCells
变量,它返回一个普通的二维值数组,其格式与 Google 示例中的 cells
变量相同。
如何解决此错误并将 Sheets 数据作为表格插入到 Docs 中?
Using the code in the Google Developer help center to insert a table into a document works fine.
// Create a two-dimensional array containing the cell contents.
var cells = [
['Row 1, Cell 1', 'Row 1, Cell 2'],
['Row 2, Cell 1', 'Row 2, Cell 2']
];
// Build a table from the array.
body.appendTable(cells);
However, what I need to do is insert cells from a spreadsheet as a table to my document. I've tried following other posts from here and wrote this:
const body = DocumentApp.getActiveDocument().getBody();
const sheet = SpreadsheetApp.openById("spreadsheetID").getSheetByName("Reports");
var ssCells = sheet.getRange("A2:C5").getValues();
body.appendTable(ssCells);
but it throws the error "Exception: The parameters (number[]) don't match the method signature for DocumentApp.Body.appendTable." I've logged the ssCells
variable and it returns a normal 2d array of values, which looks in format identical to the cells
variable from the Google example.
How do I get past this error and insert Sheets data as a table to Docs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的表格之一列的数值杀死了整个事情。使用 .getDisplayValues() 返回字符串版本并且它可以工作。
One of my Sheets columns has numerical values which killed the whole thing. Using .getDisplayValues() to return the String versions and it works.