谷歌电子表格列到行转换不起作用

时间:2018-05-28 22:25:01

标签: google-sheets

我有一列测试数据(我减少了我的实际应用) 一 二 三 四 五 并命名列的范围'列。我在名单上有另一个名字' rowBegin'。我想将列作为从rowBegin开始的行。



function columnToRow() {
  var ss = SpreadsheetApp.getActive() ;
  var sheet = ss.getActiveSheet() ;
  var column = [] ;
  column = ss.getRange('Column').getValues() ;
  var row = [] ;
  for ( i = 0 ; i < column.length ; i ++ ) {
    Logger.log('column [0,' + i + "] = " + column[0,i]);
    row [i,0] = column[0,i] ;
  }
  Logger.log('column length = ' + column.length);
  for ( i = 0 ; i < column.length ; i++ )
      Logger.log('row [' + i + ",0] = " + row[i,0]);
  
  var rowBegin = ss.getRange('RowBegin');
  var rowStart = rowBegin.getRow();
  var colStart = rowBegin.getColumn();
  var rangeRow = sheet.getRange(rowStart, colStart, 1, column.length ) ;
  rangeRow.setValues(row);
}
&#13;
&#13;
&#13;

记录器输出

[18-05-28 15:11:47:534 PDT] column [0,0] = One
[18-05-28 15:11:47:535 PDT] column [0,1] = Two
[18-05-28 15:11:47:535 PDT] column [0,2] = Three
[18-05-28 15:11:47:536 PDT] column [0,3] = Four
[18-05-28 15:11:47:536 PDT] column [0,4] = Five
[18-05-28 15:11:47:537 PDT] column length = 5
[18-05-28 15:11:47:537 PDT] row [0,0] = Five
[18-05-28 15:11:47:538 PDT] row [1,0] = Five
[18-05-28 15:11:47:538 PDT] row [2,0] = Five
[18-05-28 15:11:47:539 PDT] row [3,0] = Five
[18-05-28 15:11:47:540 PDT] row [4,0] = Five

那么为什么简单的列/行反转不起作用呢? 最终的setValues失败,&#34;范围宽度不正确,为1但应为5 ...&#34;

1 个答案:

答案 0 :(得分:0)

这有效:

myfile.txt

只需将列值加载到1维数组中,然后将其作为另一个数组的值插入,最后使其成为正确形状的2D。