简体   繁体   English

如何使用 Google Sheets 中的数据将数组插入到 Google Doc 中?

[英]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); }

Modification points:改装要点:

  • I think that there 2 reasons in your issue.我认为您的问题有两个原因。
    1. Values retrieved by getValues() is 2 dimensional array. getValues()检索到的值是二维数组。
    2. data of appendTable(data) is required to be 2 dimensional array. dataappendTable(data)必须为2维阵列。
      • In your script, fullNames is 1 dimensional array.在您的脚本中, fullNames是一维数组。 By this, such error occurs.这样,就会发生这样的错误。
  • In your script, the values are retrieved 2 columns using 2 getValues() .在您的脚本中,使用 2 getValues()检索 2 列的值。 In this case, the cost will become a bit high.在这种情况下,成本会变得有点高。 You can retrieve the values using one getValues() .您可以使用一个getValues()检索值。

When these points are reflected to your script, it becomes as follows.当这些点反映到你的脚本中时,它变成如下。

Modified script:修改后的脚本:

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);
}

References:参考:

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 的回答中提到的,还有其他“改进机会”

  1. Reduce the number of calls to the Google Apps Script Classes and Methods减少对 Google Apps 脚本类和方法的调用次数
  2. Use better ways to manage Arrays and to concatenate strings.使用更好的方法来管理数组和连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌脚本从谷歌表中提取特定列到谷歌文档? - How do I extract specific columns from google sheets to google doc using Google Script? 如何从 Google 表格运行 Google Doc ID 循环 - How to run a loop of Google Doc IDs from Google Sheets 如何使用 Google Script 从 Google 表格中提取特定列? - How do I extract a specific Column from google sheets using Google Script? 如何从谷歌表中获取数据作为字典数组 - How to get data from google sheets as an array of dictionary 如何将 Google 表格数据导入为可用的 json 值? - How do I import Google sheets data as a usable json value? 如何获取 JSON 数据并将其解析为 Google 表格脚本? - How do I fetch and parse JSON data to Google Sheets Script? 如何使用数组或对象中的值在 Google 表格中动态创建下拉列表? - How do you dynamically create a dropdown list in Google sheets using values from an array or perhaps an object? 如何使用谷歌脚本自动将表单响应从谷歌表格移动到谷歌文档? - How can I automate moving form responses from a google sheet to a google doc using google scripts? 在JavaScript中将数据插入Google表格 - Insert data to Google sheets in javascript 将Google表格中的JSON数据放入数组中 - Putting JSON data from Google sheets into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM