Google sheets v4 API returns 空数组,一行不包含任何元素
Google sheets v4 API returns empty array for a row containing no elements
我正在使用 google sheets api 来获取某些列的所有值(我正在使用 nodejs)。但是,我面临的问题是,它 returns 没有元素的行的空数组。这使得解析数据变得更加困难。有没有办法用空字符串而不是空数组来创建数组?我希望我能通过举个例子把这个问题说清楚。
假设我有一个sheet如下:
| A | B | C | D |
---+---+--------+---+--------+
1 | | Value1 | | Value2 |
---+---+--------+---+--------+
2 | | Value3 | | |
---+---+--------+---+--------+
3 | | | | |
---+---+--------+---+--------+
4 | | | | Value4 |
---+---+--------+---+--------+
当我使用 sheets.spreadsheets.values.get()
时,我得到
[
['', 'Value1', '', 'Value2'],
['', 'Value3'],
[],
['', '', '', 'Value4']
]
我想得到的是
[
['', 'Value1', '', 'Value2'],
['', 'Value3', '', ''],
['', '', '', ''],
['', '', '', 'Value4']
]
问题和解决方法:
不幸的是,当使用SheetsAPI中spreadsheets.values.get的方法时,数值如下
[
['', 'Value1', '', 'Value2'],
['', 'Value3'],
[],
['', '', '', 'Value4']
]
看来这是目前的规格。另一方面,当使用 the Spreadsheet service for Google Apps Script 时,您的目标可以直接实现。因此,为了使用表格 API 实现您的目标,在此答案中,我想提出一个解决方法。
在此解决方法中,您的目标是通过 spreadsheets.values.get 方法检索值的 post 过程实现的。
示例脚本:
根据你的问题,我认为你正在为 Node.js 使用 googleapis。以下示例脚本适用于它。
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const range = "###"; // Please set the range.
const sheets = google.sheets({version: "v4", auth: auth,});
const request = {spreadsheetId: spreadsheetId, range: range,};
sheets.spreadsheets.values.get(request, (err, { data }) => {
if (err) {
console.log(err);
return;
}
const max = data.values.reduce((c, e) => {
const len = e.length;
return c < len ? len : c;
}, 0);
const values = data.values.map((e) => {
const len = e.length;
return len < max ? e.concat(Array(max - len).fill("")) : e;
});
console.log(values); // You can see the result value here.
});
- 在此示例脚本中,首先从电子表格中检索值。并且,从检索到的值中检索列的最大长度。然后,为每一行添加空元素。
结果:
当上面的脚本是 运行 用于您的示例电子表格时,将获得以下结果。
[
[ '', 'Value1', '', 'Value2' ],
[ '', 'Value3', '', '' ],
[ '', '', '', '' ],
[ '', '', '', 'Value4' ]
]
注:
- 在这个答案中,假设您已经能够使用 Sheets API.
从 Google 电子表格中获取值
参考:
我正在使用 google sheets api 来获取某些列的所有值(我正在使用 nodejs)。但是,我面临的问题是,它 returns 没有元素的行的空数组。这使得解析数据变得更加困难。有没有办法用空字符串而不是空数组来创建数组?我希望我能通过举个例子把这个问题说清楚。
假设我有一个sheet如下:
| A | B | C | D |
---+---+--------+---+--------+
1 | | Value1 | | Value2 |
---+---+--------+---+--------+
2 | | Value3 | | |
---+---+--------+---+--------+
3 | | | | |
---+---+--------+---+--------+
4 | | | | Value4 |
---+---+--------+---+--------+
当我使用 sheets.spreadsheets.values.get()
时,我得到
[
['', 'Value1', '', 'Value2'],
['', 'Value3'],
[],
['', '', '', 'Value4']
]
我想得到的是
[
['', 'Value1', '', 'Value2'],
['', 'Value3', '', ''],
['', '', '', ''],
['', '', '', 'Value4']
]
问题和解决方法:
不幸的是,当使用SheetsAPI中spreadsheets.values.get的方法时,数值如下
[
['', 'Value1', '', 'Value2'],
['', 'Value3'],
[],
['', '', '', 'Value4']
]
看来这是目前的规格。另一方面,当使用 the Spreadsheet service for Google Apps Script 时,您的目标可以直接实现。因此,为了使用表格 API 实现您的目标,在此答案中,我想提出一个解决方法。
在此解决方法中,您的目标是通过 spreadsheets.values.get 方法检索值的 post 过程实现的。
示例脚本:
根据你的问题,我认为你正在为 Node.js 使用 googleapis。以下示例脚本适用于它。
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const range = "###"; // Please set the range.
const sheets = google.sheets({version: "v4", auth: auth,});
const request = {spreadsheetId: spreadsheetId, range: range,};
sheets.spreadsheets.values.get(request, (err, { data }) => {
if (err) {
console.log(err);
return;
}
const max = data.values.reduce((c, e) => {
const len = e.length;
return c < len ? len : c;
}, 0);
const values = data.values.map((e) => {
const len = e.length;
return len < max ? e.concat(Array(max - len).fill("")) : e;
});
console.log(values); // You can see the result value here.
});
- 在此示例脚本中,首先从电子表格中检索值。并且,从检索到的值中检索列的最大长度。然后,为每一行添加空元素。
结果:
当上面的脚本是 运行 用于您的示例电子表格时,将获得以下结果。
[
[ '', 'Value1', '', 'Value2' ],
[ '', 'Value3', '', '' ],
[ '', '', '', '' ],
[ '', '', '', 'Value4' ]
]
注:
- 在这个答案中,假设您已经能够使用 Sheets API. 从 Google 电子表格中获取值