[英]Transpose column to row google app script
I tried to transpose the column data into row.我试图将列数据转换为行。 But this code is transposing to only one row.但是此代码仅转换为一行。 For example: In my code, only the data that is in the first row of Column3 Column4 Column5 Column6 of sheet 1 (IJKL) is being transferred to the first row of row1 row2 row3 row4 of sheet 2. With this, the data contained in Column1 and Column2 of sheet 1 (AB C DE) (FGH) is being transferred to the first row of row2 row5 row6 row7 row8 row9 row10 row10 row11 row12 of sheet 2. But the problem is, the data in the second row of Column3, Column4, Column5 of Sheet 1 (MNOP) is transferred to the second row of row2, row2, row3, row4 of Sheet 2, but the data in Column1 and Column2 of Sheet 1 (AB C DE) (FGH) it is not being moved to the second row of sheet2 row5 row6 row7 row8 row9 row10 row10 row11 row12.例如:在我的代码中,只有工作表 1 (IJKL) 的 Column3 Column4 Column5 Column6 的第一行中的数据被传输到工作表 2 的 row1 row2 row3 row4 的第一行。这样,包含在工作表 1 的 Column1 和 Column2 (AB C DE) (FGH) 正在转移到工作表 2 的 row2 row5 row6 row7 row8 row9 row10 row10 row11 row12 的第一行。但问题是,Column3 的第二行中的数据, Sheet 1(MNOP)的Column4,Column5被转移到Sheet 2的row2,row2,row3,row4的第二行,但是Sheet 1的Column1和Column2中的数据(AB C DE)(FGH)它没有被移动到 sheet2 row5 row6 row7 row8 row9 row10 row10 row11 row12 的第二行。 However, if you run the script a second time, the same thing is happening again.但是,如果您再次运行该脚本,同样的事情会再次发生。 Now all I want is to move the data that is in (AB C DE) (FGH) in Column1 and Column2 of sheet 1 to the first row of row 2 row5 row6 row7 row8 row9 row10 row10 row11 row12 and the second row to the second row (MNOP).现在我想要的是将工作表 1 的 Column1 和 Column2 中的 (AB C DE) (FGH) 中的数据移动到第 2 行的第一行 row5 row6 row7 row8 row9 row10 row10 row11 row12 和第二行到第二行行 (MNOP)。
The data contained in columns 1 and 2 of sheet 1 should be transposed to sheet 2 along with the data from columns 3 to 6 each time the script is being executed.每次执行脚本时,应将工作表 1 的第 1 列和第 2 列中包含的数据与第 3 到 6 列中的数据一起转置到工作表 2。
function transpose() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheetByName('Sheet1');
var sh2 = ss.getSheetByName('Sheet2');
var lr = sh2.getLastRow()+1;
var gv1 = sh1.getRange('D10:G13').getValues().filter(r => r.some(e => e));
var gv2 = sh1.getRange('A2:A6').getValues();
var gv3 = sh1.getRange('B3:B5').getValues();
var result1 = [];
var result2 = [];
for(var col = 0; col < gv2[0].length; col++){
result1[col]=[];
for(var row = 0; row < gv2.length; row++){
result1[col][row] = gv2[row][col];
}
}
for(var col = 0; col < gv3[0].length; col++){
result2[col]=[];
for(var row = 0; row < gv3.length; row++){
result2[col][row] = gv3[row][col];
}
var sv1 = sh2.getRange(lr,1,gv1.length,gv1[0].length).setValues(gv1);
var sv2 = sh2.getRange(lr,5,result1.length,result1[0].length).setValues(result1);
var sv3 = sh2.getRange(lr,10,result2.length,result2[0].length).setValues(result2);
}
Not sure if I understand the goal.不确定我是否理解目标。
Do you want this?你想要这个吗?
Here is the code that does this:这是执行此操作的代码:
function transpose() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheetByName('Sheet1');
var sh2 = ss.getSheetByName('Sheet2');
var lr = sh2.getLastRow()+1;
var gv1 = sh1.getRange('D10:G13').getValues().filter(r => r.some(e => e));
var gv2 = sh1.getRange('A2:A6').getValues();
var gv3 = sh1.getRange('B3:B5').getValues();
var result1 = [...gv1[0], ...gv2.flat(), ...gv3.flat()];
var result2 = [...gv1[1], ...gv2.flat(), ...gv3.flat()];
var results = [result1, result2];
sh2.getRange(2,1,results.length, results[0].length).setValues(results);
}
function coltorow() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet Name");//add sheet name
return [sh.getRange(1,col,sh.getLastRow(),1).getValues().flat()];//edit col
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.