简体   繁体   English

Google Apps脚本-sheet.getDataRange()。getValues(); 列作为对象

[英]Google Apps Script - sheet.getDataRange().getValues(); columns as objects

I have a sheet with multiple columns and rows. 我有一个包含多个列和行的工作表。 I want to read data from the sheet by column and not by row. 我想按列而不是按行从工作表中读取数据。 The

sheet.getDataRange().getValues();

function returns an array like this [ row1[column, column..], row2[column, column..]] etc. What I want to do is get an array like: [column1[row, row..], column2[row, row..]] etc. 函数返回一个像这样的数组[row1 [column,column ..],row2 [column,column ..]]等。我想要做的就是得到一个数组,例如:[column1 [row,row ..],column2 [行,行。]]]等

It's worth to mention that the columns does not have the same number of rows. 值得一提的是,列的行数不同。

Do you guys have any ideas on how to achieve this? 你们对如何实现这一目标有任何想法吗?

Thanks! 谢谢!

//* View Logger.log output in the Script Editor View > Logs

function test(a){
  sheet = SpreadsheetApp.getActiveSheet() 
  range = sheet.getDataRange().getValues();
  Logger.log(range);
  transposeRange = Transpose(range);
  Logger.log(transposeRange);
}

function Transpose(a){
  return Object.keys(a[0]).map( function (c) { return a.map(function (r) { return r[c];}); });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM