Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google应用程序脚本:如何将对象数组复制到范围?_Javascript_Arrays_Object_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript Google应用程序脚本:如何将对象数组复制到范围?

Javascript Google应用程序脚本:如何将对象数组复制到范围?,javascript,arrays,object,google-apps-script,google-sheets,Javascript,Arrays,Object,Google Apps Script,Google Sheets,我有一个名为成员资格的对象数组: [{name: 'Linus Pauling', address: '1805 Main Street', phone: '(615) 555-1010', email: 'linus.pauling@gmail.com' }, {name: 'Maury Povich', address: '382 North Street', phone: '(423) 555-1997', email: 'maury@themauryshow.org'}]

我有一个名为
成员资格
的对象数组:

[{name: 'Linus Pauling', address: '1805 Main Street', phone: '(615) 555-1010', 
  email: 'linus.pauling@gmail.com' },

 {name: 'Maury Povich', address: '382 North Street', phone: '(423) 555-1997', 
  email: 'maury@themauryshow.org'}]
(虽然这里只显示了4个,但每个成员实际上有10个键/值对。)

将此数组的位复制到Google工作表中的一系列单元格的最佳方法是什么?我试图找到一种方法来直接调用对象值,并使用.SetValues方法整体复制它们,而不是一次复制一个

要获取A列中所有成员的姓名,我已尝试:

sheet.getRange(1,1,membership.length,1).setValues(membership[{member.name}]);
…在属性ID之后提供
缺失。

或:

…这导致ReferenceError:“名称”未定义

或:

…这会导致“无法将数组转换为对象[][]”错误


我为这个新手问题道歉。我已经了解了如何将值从多维数组复制到工作表的范围,而不是对象数组。

下面的脚本如何?此脚本是电子表格的容器绑定脚本。有两种模式。模式1和模式2之间的数据排列不同

模式1是

模式2是


如果我对您的问题的理解有误,我很抱歉。

您是否希望生成以下表格:

name | Address | phone | email
-----+---------+-------+------
.... | ....    | ....  | ....
等等

如果是这样,那么下面的代码片段可能会有所帮助。这里要指出的关键点是,在迭代对象时,不能期望给定的顺序。i、 e.仅仅因为你对
会员资格的表述
首先列出了
姓名
,并不意味着如果你将
用于。。。在
循环中,您可以保证
name
将首先返回-JavaScript中的对象是无序的

为了确保给定的顺序,snippet I列表定义了一个数组
headers
,您可以在其中指定要在工作表中显示的列的顺序。这用于保证输出中的列顺序:

var membership = [
  {
    name: 'Linus Pauling', address: '1805 Main Street', phone: '(615) 555-1010', 
    email: 'linus.pauling@gmail.com'
  },
  {
    name: 'Maury Povich', address: '382 North Street', phone: '(423) 555-1997', 
    email: 'maury@themauryshow.org'
  }
];

// Headings in the column order that you wish the table to appear.
var headings = ['name', 'address', 'phone', 'email'];
var outputRows = [];

// Loop through each member
membership.forEach(function(member) {
  // Add a new row to the output mapping each header to the corresponding member value.
  outputRows.push(headings.map(function(heading) {
    return member[heading] || '';
  }));
});

// Write to sheets
if (outputRows.length) {
  // Add the headings - delete this next line if headings not required
  outputRows.unshift(headings);
  SpreadsheetApp.getActiveSheet().getRange(1, 1, outputRows.length, outputRows[0].length).setValues(outputRows);
}
输出为:


有关检索数据的有用代码,请参阅的底部的脚本。从该注释向下复制所有代码:

//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
/////////

/////////////////////////////////////////////////////////////////////////////////

// getRowsData iterates row by row in the input range and returns an array of objects.

一旦完成,getRowsData不仅返回数据,还返回第二项,即标题列表。使用此选项,您可以修改@Bardy中的代码,以允许以任何顺序显示标题,以及中间可能的其他列。

这可能有助于从JSON对象插入一行数据:谢谢。我完全不知道
map
方法,这正是这里需要的。
name | Address | phone | email
-----+---------+-------+------
.... | ....    | ....  | ....
var membership = [
  {
    name: 'Linus Pauling', address: '1805 Main Street', phone: '(615) 555-1010', 
    email: 'linus.pauling@gmail.com'
  },
  {
    name: 'Maury Povich', address: '382 North Street', phone: '(423) 555-1997', 
    email: 'maury@themauryshow.org'
  }
];

// Headings in the column order that you wish the table to appear.
var headings = ['name', 'address', 'phone', 'email'];
var outputRows = [];

// Loop through each member
membership.forEach(function(member) {
  // Add a new row to the output mapping each header to the corresponding member value.
  outputRows.push(headings.map(function(heading) {
    return member[heading] || '';
  }));
});

// Write to sheets
if (outputRows.length) {
  // Add the headings - delete this next line if headings not required
  outputRows.unshift(headings);
  SpreadsheetApp.getActiveSheet().getRange(1, 1, outputRows.length, outputRows[0].length).setValues(outputRows);
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
/////////

/////////////////////////////////////////////////////////////////////////////////

// getRowsData iterates row by row in the input range and returns an array of objects.