说我有一个范围,“A3:C5”这样:
Bob,Cob,Rob
Joe,Foe,Boe
Jack,Back,Sack
我希望在Google表格中重新格式化
Bob,Cob,Rob
Boe,Foe,Joe
Back,Jack,Sack
换句话说,我希望数据只按字母顺序排列在它们所在的行中。我不能为我的生活弄清楚如何在谷歌应用程序脚本中执行此操作,是否有人能够提供帮助?
答案 0 :(得分:0)
快速谷歌搜索:
function sortHorizontal()
{
var startCol = 1; // left-most column to where the sort will be applied; A = 1, B = 2, C = 3 etc
var startRow = 1; // first row to apply the sort
var sheetName = 'Sheet1'; // name of sheet in which sort will be applied
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var d = s.getDataRange().getValues();
var output = [], temp;
for (var i = startRow - 1; i < d.length; i++)
{
temp = d[i].slice(startCol - 1);
temp.sort(ascSort); // change to descSort for Z -> A sort
output.push(temp);
}
s.getRange(startRow, startCol, output.length, output[0].length).setValues(output);
}
function ascSort(a, b)
{
return ((a == b) ? 0 : (a && (!b || a < b)) ? -1 : 1);
}
function descSort(a, b)
{
return ((a == b) ? 0 : (a > b) ? -1 : 1);
}