在 Google 个应用脚本中隐藏列数组
Hide an array of columns in Google App Scripts
这里是初级程序员,如果我在解释我的问题时词汇不是很准确,请提前道歉。
我有一个脚本可以创建自定义菜单(视图)并根据您要查看的视图隐藏不同的列集。
如何更改脚本,而不是键入我所有的列,我可以获得列数组(在本例中为数组 [1 到 4] 和 [7 到 13])?
在我想要更改的代码的特定部分下方
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: [1, 2, 3, 4, 7, 8, 9, 11, 12, 13 ] }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
这里是完整的脚本,如果它有助于解决我的问题
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom View')
.addItem('All - 30 days', 'hideColumnsAll30')
.addItem('All - 1 year', 'hideColumnsAll1y')
.addItem('Unhide All', 'showColumns')
.addToUi();
function showColumns() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
showColumnsInAllSheets_(sheets);
}
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: [1, 2, 3, 4, 7, 8, 9, 11, 12, 13 ] }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
function hideColumnsAll1y() {
const obj = [{ sheetName: "Planning / Tracking", hide: [1, 3, 4, 5, 6, 7, 8, 9, 11, 12, ] }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
在此先感谢您的帮助!
根据您的以下回复,
I need to type all the columns manually -> [1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13] I would like to have 2 arrays, like this -> [1 - 4], [7 -13]
我猜你想通过使用像 [1 - 4], [7 -13]
这样的 2 个数组来检索 [1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13]
的数组。如果我的理解是正确的,下面的示例脚本怎么样?
示例脚本:
const input = [[1, 4], [7, 13]]; // 1st and 2nd element of each array are start and end values, respectively.
const res = input.flatMap(([start, end]) => [...Array(end)].map((_, i) => i + 1).slice(start - 1, end));
console.log(res);
- 当这个脚本为运行时,得到一个
[ 1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13 ]
的数组
参考文献:
已添加:
根据您的以下回复,
I actually found similar piece of code to add to my script online. However it does not fit my case as I need to give the name of each sheets where I want to hide columns.
并且,在您提供的脚本中,我发现了以下消息。
I have other speadsheets with 1000+ columns, so I cannot hide them manually from 1 to 1000. This is the reasons why I'm trying to include several arrays in my script
在这种情况下,以下脚本是您期望的脚本吗?
const columns = input => input.flatMap(([start, end]) => [...Array(end)].map((_, i) => i + 1).slice(start - 1, end));
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: columns([[1, 4], [7, 13]]) }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
- 在
hide: columns([[1, 4], [7, 13]])
时,创建了 hide: [ 1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13 ]
。
建议
在你的脚本中,我不确定你将在哪里使用数组 1-4 和 7-13,因为在脚本中,特别是在 { sheetName: "Planning / Tracking", hide: [1, 2, 3, 4, 7, 8, 9, 11, 12, 13 ] }
部分,你在这里跳过了数字 10,而在另一个函数中你跳过 2.
下面是关于如何将数组用于函数的建议。您可以在函数外部添加一个新变量使其成为全局变量,在这里您将插入所需的数组值。然后,您可以在您的任何函数中访问这些变量,因此您只需编辑一行,以防您调整列。
试试这个代码:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom View')
.addItem('All - 30 days', 'hideColumnsAll30')
.addItem('All - 1 year', 'hideColumnsAll1y')
.addItem('Unhide All', 'showColumns')
.addToUi();
};
//Declare arrays here***
//Put in array the columns for Resources and Documentation sheets
var resDocArray = [2,4,5,9];
//Put in array the columns for the planning/tracking tab for 2 different functions
var colAll30d = [1, 2, 3, 4, 7, 8, 9, 11, 12, 13];
var colAll1y = [1, 3, 4, 5, 6, 7, 8, 9, 11, 12]
//***
function showColumns() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
showColumnsInAllSheets_(sheets);
};
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: colAll30d }, { sheetName: "Resources", hide: resDocArray}, {sheetName: "Documentation", hide:resDocArray} ];
sample_(obj);
};
function hideColumnsAll1y() {
const obj = [{ sheetName: "Planning / Tracking", hide: colAll1y }, { sheetName: "Resources", hide: resDocArray}, {sheetName: "Documentation", hide:resDocArray} ];
sample_(obj);
};
function sample_(obj) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
showColumnsInAllSheets_(sheets);
const sheetObj = sheets.reduce((o, s) => (o[s.getSheetName()] = s, o), {});
obj.forEach(({ sheetName, hide }) => {
if (sheetObj[sheetName]) {
hide.forEach(h => sheetObj[sheetName].hideColumns(h, 1));
}
});
};
function showColumnsInAllSheets_(sheets) {
sheets.forEach(s => s.showColumns(1, s.getMaxColumns()));
};
您也可以按照 Tanaike 建议的方式添加数组。
如果有帮助请告诉我!
这里是初级程序员,如果我在解释我的问题时词汇不是很准确,请提前道歉。
我有一个脚本可以创建自定义菜单(视图)并根据您要查看的视图隐藏不同的列集。
如何更改脚本,而不是键入我所有的列,我可以获得列数组(在本例中为数组 [1 到 4] 和 [7 到 13])?
在我想要更改的代码的特定部分下方
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: [1, 2, 3, 4, 7, 8, 9, 11, 12, 13 ] }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
这里是完整的脚本,如果它有助于解决我的问题
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom View')
.addItem('All - 30 days', 'hideColumnsAll30')
.addItem('All - 1 year', 'hideColumnsAll1y')
.addItem('Unhide All', 'showColumns')
.addToUi();
function showColumns() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
showColumnsInAllSheets_(sheets);
}
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: [1, 2, 3, 4, 7, 8, 9, 11, 12, 13 ] }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
function hideColumnsAll1y() {
const obj = [{ sheetName: "Planning / Tracking", hide: [1, 3, 4, 5, 6, 7, 8, 9, 11, 12, ] }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
在此先感谢您的帮助!
根据您的以下回复,
I need to type all the columns manually -> [1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13] I would like to have 2 arrays, like this -> [1 - 4], [7 -13]
我猜你想通过使用像 [1 - 4], [7 -13]
这样的 2 个数组来检索 [1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13]
的数组。如果我的理解是正确的,下面的示例脚本怎么样?
示例脚本:
const input = [[1, 4], [7, 13]]; // 1st and 2nd element of each array are start and end values, respectively.
const res = input.flatMap(([start, end]) => [...Array(end)].map((_, i) => i + 1).slice(start - 1, end));
console.log(res);
- 当这个脚本为运行时,得到一个
[ 1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13 ]
的数组
参考文献:
已添加:
根据您的以下回复,
I actually found similar piece of code to add to my script online. However it does not fit my case as I need to give the name of each sheets where I want to hide columns.
并且,在您提供的脚本中,我发现了以下消息。
I have other speadsheets with 1000+ columns, so I cannot hide them manually from 1 to 1000. This is the reasons why I'm trying to include several arrays in my script
在这种情况下,以下脚本是您期望的脚本吗?
const columns = input => input.flatMap(([start, end]) => [...Array(end)].map((_, i) => i + 1).slice(start - 1, end));
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: columns([[1, 4], [7, 13]]) }, { sheetName: "Resources", hide: [2, 4, 5, 9]}, {sheetName: "Documentation", hide:[2, 4, 5, 9]} ];
sample_(obj);
}
- 在
hide: columns([[1, 4], [7, 13]])
时,创建了hide: [ 1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13 ]
。
建议
在你的脚本中,我不确定你将在哪里使用数组 1-4 和 7-13,因为在脚本中,特别是在 { sheetName: "Planning / Tracking", hide: [1, 2, 3, 4, 7, 8, 9, 11, 12, 13 ] }
部分,你在这里跳过了数字 10,而在另一个函数中你跳过 2.
下面是关于如何将数组用于函数的建议。您可以在函数外部添加一个新变量使其成为全局变量,在这里您将插入所需的数组值。然后,您可以在您的任何函数中访问这些变量,因此您只需编辑一行,以防您调整列。
试试这个代码:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom View')
.addItem('All - 30 days', 'hideColumnsAll30')
.addItem('All - 1 year', 'hideColumnsAll1y')
.addItem('Unhide All', 'showColumns')
.addToUi();
};
//Declare arrays here***
//Put in array the columns for Resources and Documentation sheets
var resDocArray = [2,4,5,9];
//Put in array the columns for the planning/tracking tab for 2 different functions
var colAll30d = [1, 2, 3, 4, 7, 8, 9, 11, 12, 13];
var colAll1y = [1, 3, 4, 5, 6, 7, 8, 9, 11, 12]
//***
function showColumns() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
showColumnsInAllSheets_(sheets);
};
function hideColumnsAll30() {
const obj = [{ sheetName: "Planning / Tracking", hide: colAll30d }, { sheetName: "Resources", hide: resDocArray}, {sheetName: "Documentation", hide:resDocArray} ];
sample_(obj);
};
function hideColumnsAll1y() {
const obj = [{ sheetName: "Planning / Tracking", hide: colAll1y }, { sheetName: "Resources", hide: resDocArray}, {sheetName: "Documentation", hide:resDocArray} ];
sample_(obj);
};
function sample_(obj) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
showColumnsInAllSheets_(sheets);
const sheetObj = sheets.reduce((o, s) => (o[s.getSheetName()] = s, o), {});
obj.forEach(({ sheetName, hide }) => {
if (sheetObj[sheetName]) {
hide.forEach(h => sheetObj[sheetName].hideColumns(h, 1));
}
});
};
function showColumnsInAllSheets_(sheets) {
sheets.forEach(s => s.showColumns(1, s.getMaxColumns()));
};
您也可以按照 Tanaike 建议的方式添加数组。
如果有帮助请告诉我!