谷歌应用程序脚本:自动排序函数与范围选定的工作表



我试图实现的结果是根据日期以升序自动排序整行,包括新条目。现在我只能应用到1个GSheet选项卡,我试图应用到其他选定的表选项卡,但找不到方法。有什么解决办法吗?谢谢你!

SHEET_NAME = "Sheet1";
SORT_DATA_RANGE = "A2:Z";
SORT_ORDER = [
{column: 3, ascending: true}, // 3 = column number, sort by ascending order  
// {column: 3, ascending: false}, // 3 = column number, sort by decending order  
];

//Note pop-up appear when Sorting is completed
function onEdit(e){
multiSortColumns();
}
function multiSortColumns(){
var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName(SHEET_NAME);
var range = sheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
ss.toast('Sort action executed.');
}

要按指定范围和选项对所有工作表进行排序,请尝试:

function multiSortColumns() {

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
spreadsheet.getSheets()
.forEach(sheet => sheet.getRange(SORT_DATA_RANGE).sort(SORT_ORDER))
spreadsheet.toast('Sort action executed.')
}

要对特定的工作表进行排序,请尝试:

function multiSortColumns() {

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
const targetSheets = [`Sheet1`, `Sheet2`, `Sheet3`]
targetSheets.forEach(sheetName => {
spreadsheet.getSheetByName(sheetName)
.getRange(SORT_DATA_RANGE)
.sort(SORT_ORDER)
})
spreadsheet.toast('Sort action executed.')
}

相关内容

  • 没有找到相关文章

最新更新