[英]How to sort each row of a Google Sheet in Alphabetical Order
假設我有一個范圍“ A3:C5”,這樣:
鮑勃,柯布,羅布
喬,敵人,Boe
傑克,背部,麻袋
我希望在Google工作表中將其重新格式化為
鮑勃,柯布,羅布
鮑,敵人,喬
回來,傑克,麻袋
換句話說,我希望僅按字母順序對數據進行排序。 我一輩子都無法弄清楚如何在Google Apps腳本中執行此操作,任何人都可以提供幫助嗎?
快速Google搜索:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.