這里的初學者程式員,如果在解釋我的問題時我的詞匯不是很準確,請提前道歉。
我有一個腳本,它創建一個自定義選單(視圖)并根據您想要查看的視圖隱藏不同的列集。
如何更改腳本,而不是鍵入所有列,我可以獲得列陣列(在本例中為陣列 [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);
}
在此先感謝您的幫助!
uj5u.com熱心網友回復:
從您的以下回復中,
我需要手動輸入所有列 -> [1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13] 我想要 2 個陣列,像這樣 -> [1 - 4] , [7 -13]
我猜你想[1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13]通過使用 2 個陣列來檢索一個陣列,例如[1 - 4], [7 -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 ]。
參考:
- 地圖()
- 平面圖()
添加:
從您的以下回復中,
我實際上在網上找到了類似的代碼添加到我的腳本中。但是它不適合我的情況,因為我需要給出要隱藏列的每張作業表的名稱。
而且,在您提供的腳本中,我發現了以下訊息。
我有其他包含 1000 多列的電子表格,因此我無法手動將它們從 1 隱藏到 1000。這就是我嘗試在腳本中包含多個陣列的原因
在這種情況下,以下腳本是您預期的腳本嗎?
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 ]被創建。
uj5u.com熱心網友回復:
建議
在您的腳本中,我不確定您將在哪里使用陣列 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 建議的方式添加陣列。
讓我知道這是否有幫助!
參考: 隱藏多個作業表中的列
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463917.html
標籤:谷歌应用脚本
下一篇:將字串轉換為10鍵電話鍵盤數字
