我有一个脚本,其中Google工作表的行数据存储在JSON中。我想过滤存储在数组中的数据。我不希望将空单元格插入到数组中。我只想将填充的单元格插入到数组中。这是我正在使用的脚本
var dataArray = [];
var rows = sheet.getRange(2,1,sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
for(var i = 0, l= rows.length; i<l ; i++){
var dataRow = rows[i];
var record = {};
record['name'] = dataRow[0];
record['age'] = dataRow[1];
record['weight'] = dataRow[2];
dataArray.push(record);
}
答案 0 :(得分:2)
我认为您真正要寻找的是.filter
方法来清除空单元格,然后可以使用.map
对其进行转换:
var rows = sheet.getRange(
2,
1,
sheet.getLastRow() - 1,
sheet.getLastColumn()
).getValues();
var filteredDataArray = rows.filter(function(row) {
return row[0] !== ''; // Based on your comment that "empty" means it's an empty string
});
var transformedDataArray = filteredDataArray.map(function(row) {
return { name: row[0], age: row[1], weight: row[2] };
});