应用于工作表中的特定选项卡 [英] Apply to Specific Tabs within a Sheet
问题描述
我的电子表格中有9个标签,并且只想对其中的6个标签应用排序脚本.我不知道如何做到这一点.
I have 9 tabs within my spreadsheet, and would like to apply a sorting script to only 6 of those tabs. I can't figure out how to make that happen.
///////Auto Sort Phase Sheets
//get sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ['Phase 1','Phase 2', 'Phase 3', 'Phase 4', 'Phase 5', 'Phase 6'];
//get active cell
var editedCell = sheet.getActiveCell();
//get range
var range = sheet.getRange("A2:Z");
//sort by: status, priority, estimated time
range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}, {column:3}]);
我收到此错误:
TypeError:在对象阶段1,阶段2,阶段3,阶段4,阶段5,阶段6
TypeError: Cannot find function getActiveCell in object Phase 1,Phase 2,Phase 3,Phase 4,Phase 5,Phase 6
当我删除"getActiveCell"时,出现此错误:TypeError:在对象阶段1,阶段2,阶段3,阶段4,阶段5,阶段6中找不到函数getRange.
When I remove 'getActiveCell' I get this error: TypeError: Cannot find function getRange in object Phase 1,Phase 2,Phase 3,Phase 4,Phase 5,Phase 6.
当我删除'getRange'时,脚本什么都不做.
When I remove 'getRange' the script just does nothing.
推荐答案
我认为主要的问题是您需要循环遍历工作表"数组中指定的工作表名称的指定列表.除此之外,共享的代码还应嵌入到函数中才能运行(顺便说一下,我删除了editedCell变量,因为它是多余的):
I think that the primary problem was that you need to loop through the specified list of sheet names given in your 'sheet' array. In addition to that, the code you shared should be embedded in a function in order to be run (by the way, I removed the editedCell variable because it is redundant):
///////Auto Sort Phase Sheets
function sortSheets(){
//get sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Specify sheets to be sorted
var sheet_name = ['Phase 1','Phase 2', 'Phase 3', 'Phase 4', 'Phase 5', 'Phase 6'];
for (s=0; s<sheet_name.length; s++) {
var sheet = ss.getSheetByName(sheet_name[s]);
//get range
var range = sheet.getRange("A2:Z");
//sort by: status, priority, estimated time
range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}, {column:3}]);
}
}
这是我对其进行测试的工作表: https://docs.google.com/spreadsheets/d/1VyhzrECw7Q7Q1Q2 #gid = 0
Here is the sheet I tested it on: https://docs.google.com/spreadsheets/d/1VyhzrYwOWgGWszrRoUrQU2ECGqwN_lfb99fqw7x9P1k/edit#gid=0
这篇关于应用于工作表中的特定选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!