[英]Google Sheets “Transpose(sort(transpose” -as Script
Is there a Google Sheets script that would be able to transpose my data, then sort it, then transpose it back.是否有一个谷歌表格脚本能够转置我的数据,然后对其进行排序,然后将其转回。 I need to do this formula below - but as a script.
我需要在下面做这个公式 - 但作为一个脚本。
=transpose(sort(transpose(J1:N4),1,)) =转置(排序(转置(J1:N4),1,))
My intention is to be able to sort my header columns A->Z so I can have a consistent placement of header columns every time I put them in. For example, sometimes the data comes as column A:G but the next time, the headers that used to be A:G are now AGBFCDE (scrambled).我的意图是能够对我的 header 列 A->Z 进行排序,这样我每次放入 header 列时都可以保持一致的位置。例如,有时数据以列 A:G 的形式出现,但下一次,以前是 A:G 的标头现在是 AGBFCDE(加扰)。 Sorting the entire columns going down is pretty strait forward but I need to sort A1:G1 while keeping the data below it still associated with the proper column header.
将整个列向下排序非常困难,但我需要对 A1:G1 进行排序,同时保持其下方的数据仍与正确的列 header 相关联。 For example, it would be "move entire column left or right" until the column headers are sorted A->Z.
例如,在列标题按 A->Z 排序之前,它将是“向左或向右移动整个列”。 I am hoping this method of transposing-sorting-transposing back will do the trick but other recommendations would be welcome.
我希望这种转置-排序-转置的方法可以解决问题,但欢迎其他建议。
column 1 column4 column5 column 2 column 3
data 1 data 4 data 5 data 2 data 3
data 1 data 4 data 5 data 2 data 3
data 1 data 4 data 5 data 2 data 3
So as you can see above, the column headers are not in order.正如您在上面看到的,列标题没有按顺序排列。 Every time I put them in, they will not be but I would like to know if there is preferably a script or some way to then sort Column 1:Column 5 in order even though they will never be pasted that way.
每次我把它们放进去时,它们都不会,但我想知道是否最好有一个脚本或某种方法来按顺序对第 1 列:第 5 列进行排序,即使它们永远不会以这种方式粘贴。 If there is a better way besides transposing/sorting then I would be open to any method.
如果除了转置/排序之外还有更好的方法,那么我将对任何方法持开放态度。
Here I created a script with the same concept of Transpose -> Sort -> Transpose.在这里,我创建了一个具有相同概念的脚本 Transpose -> Sort -> Transpose。
function myFunction() {
// get all the data in the sheet
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getDataRange();
var values = range.getValues();
//convert rows into columns
var data = transpose(values);
//sort data
data.sort(
function(a,b) {
return b[0]-a[0] || a[0].localeCompare(b[0]);
});
//convert columns into rows
var data1 = transpose(data);
//write data back to spreadsheet
ss.getRange(1,1,values.length,values[0].length)
.setValues(data1);
}
function transpose(matrix) {
return matrix[0].map((col, i) => matrix.map(row => row[i]));
}
Before前
After后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.