【问题标题】:How to reorder multiple ranges in Google Sheets with apps script?如何使用应用程序脚本在 Google 表格中重新排序多个范围?
【发布时间】:2021-12-19 20:18:28
【问题描述】:
【问题讨论】:
标签:
google-apps-script
google-sheets
【解决方案1】:
我相信你的目标如下。
- 您希望通过“J”列对 B3:J6、B9:J12、B15:J18、B21:J24、B27:J30、B33:J36、B39:J42 和 B45:J48 的范围进行排序电子表格。
在这种情况下,下面的示例脚本怎么样?
示例脚本:
function myFunction() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('###SheetName###'); // Please set the sheet name here.
const a1Notations = ["B3:J6", "B9:J12", "B15:J18", "B21:J24", "B27:J30", "B33:J36", "B39:J42", "B45:J48"];
const ranges = sheet.getRangeList(a1Notations).getRanges();
const formatRanges = a1Notations.map(r => r.replace(/^./, "J"));
sheet.getRangeList(formatRanges).setNumberFormat("0");
ranges.forEach(r => r.sort({ column: r.getLastColumn(), ascending: true }));
sheet.getRangeList(formatRanges).setNumberFormat("@");
}
参考资料:
【解决方案2】:
通过范围列表排序
function myfunction( ) {
const ss = SpreadsheetApp.openById("ssid");
const sh = ss.getSheetByName('Sheet0');
const rgl = ss.getRangeList(['B3:J6', 'B9:J12', 'B15:J18', 'B21:J24', 'B27:J30', 'B33:J36', 'B39:J42', 'B45:J48']);
//const rgl = sh.getRangeList(['B3:J6', 'B9:J12', 'B15:J18', 'B21:J24', 'B27:J30', 'B33:J36', 'B39:J42', 'B45:J48']);//You can also do it on a sheet
rgl.getRanges().forEach(r => {
let col = r.getColumn();
r.sort({column: col,sortAscending:true})
});
}