[英]How do I insert an array into a Google Doc using data from Google Sheets?
I am trying to pull a range of names from a Google sheet and place it into a Google Doc.In the spreadsheet, the last names("lastNames") come before the first names ("firstNames"), and both are in separate columns.我正在尝试从 Google 工作表中提取一系列名称并将其放入 Google Doc.In 电子表格中,姓氏(“lastNames”)出现在名字(“firstNames”)之前,并且两者都在单独的列中. I am trying to place the first and last names together into my doc with the first names first.
我正在尝试将名字和姓氏放在我的文档中,名字在前。
I used a for loop to put the first and last names together into an array ("fullNames"), and that part works just fine.我使用 for 循环将名字和姓氏放在一个数组中(“fullNames”),这部分工作得很好。 When I used Logger.log, all the first names and last names are together in an array, with each full name separated by a common, just the way I wanted them to be.
当我使用 Logger.log 时,所有的名字和姓氏都放在一个数组中,每个全名由一个公共分隔,就像我想要的那样。
What I can't figure out how to do is actually insert this new array into the body of the document.我不知道该怎么做实际上是将这个新数组插入到文档正文中。 I am using the appendTable method, but every time I try to I get the following error: "The parameters (number[]) don't match the method signature for DocumentApp.Body.appendTable."
我正在使用 appendTable 方法,但每次尝试时都会收到以下错误:“参数 (number[]) 与 DocumentApp.Body.appendTable 的方法签名不匹配。”
What changes do I have to make to my code to actually place my new array into my google doc?我必须对我的代码进行哪些更改才能将我的新数组实际放入我的 google 文档中?
function namePusher() { var ss = SpreadsheetApp.openById("1CHvnejDrrb9W5txeXVMXxBoVjLpvWSi40ehZkGZYjaY"); var lastNames = ss.getSheetByName("Campbell").getRange(2, 2, 18).getValues(); var firstNames = ss.getSheetByName("Campbell").getRange(2, 3, 18).getValues(); //Logger.log(firstNames); var fullNames = []; for(var i = 0; i < firstNames.length; i++){ var nameConcat = firstNames[i] + " " + lastNames[i] fullNames.push(nameConcat); } //Logger.log(fullNames); var doc = DocumentApp.getActiveDocument().getBody(); doc.appendTable(fullNames); }
getValues()
is 2 dimensional array. getValues()
检索到的值是二维数组。data
of appendTable(data)
is required to be 2 dimensional array. data
的appendTable(data)
必须为2维阵列。
fullNames
is 1 dimensional array.fullNames
是一维数组。 By this, such error occurs.getValues()
.getValues()
检索 2 列的值。 In this case, the cost will become a bit high.getValues()
.getValues()
检索值。 When these points are reflected to your script, it becomes as follows.当这些点反映到你的脚本中时,它变成如下。
function namePusher() {
var ss = SpreadsheetApp.openById("1CHvnejDrrb9W5txeXVMXxBoVjLpvWSi40ehZkGZYjaY");
var values = ss.getSheetByName("Campbell").getRange("B2:C19").getValues(); // Modified
var fullNames = [];
for(var i = 0; i < values.length; i++){ // Modified
var nameConcat = [values[i][1] + " " + values[i][0]]; // Modified
fullNames.push(nameConcat);
}
var doc = DocumentApp.getActiveDocument().getBody();
doc.appendTable(fullNames);
}
One simple way to fix your code is by replacing修复代码的一种简单方法是替换
fullNames.push(nameConcat);
by经过
fullNames.push([nameConcat]);
The problem with your script is that fullNames
is an Array of strings but your should pass an Array of Arrays of strings (or objects that might be coerced to strings).用你的脚本的问题是,
fullNames
是一个字符串数组,但你应该传递(也可能被迫字符串或对象)的字符串数组的数组。
Basic demo基本演示
var data = [
['A','B','C'],
[1, 'Apple','Red'],
[2, 'Banana','Yellow']
];
function myFunction() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
body.appendTable(data);
}
As mentioned on Tanaike's answer there are other "improvement opportunities"正如Tanaike 的回答中提到的,还有其他“改进机会”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.