如何添加自定义菜单以同时隐藏/显示两列?

问题描述 投票:0回答:1

我有一张多年的统计表;但是,我想隐藏未来年份以保持表格紧凑,直到需要这些年份为止。我有以下脚本,一次隐藏和显示一列,但我需要它一次隐藏两列。例如,在菜单点击时隐藏 T 和 U 列(而不是一次点击 T 和下一次点击 U )。

/** @OnlyCurrentDoc */

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Show Next Season')
      .addItem('Hide Columns', 'hideCol')
      .addItem("Show Columns", 'show')
      .addToUi();
}

function hideCol() {
  const sheet = SpreadsheetApp.getActive().getSheetByName('Season by Season'); //put the name of the sheet in place of 'Main'
  const array =  Array(sheet.getMaxColumns()-4).fill().map((element, index) => index + 20);
  for(let col of array){
     if(!sheet.isColumnHiddenByUser(col)){
         sheet.hideColumns(col);
         break;
     }
  }
}

function show() {
  const sheet = SpreadsheetApp.getActive().getSheetByName('Season by Season'); //put the name of the sheet in place of 'Main'
  const array =  Array(sheet.getMaxColumns()-4).fill().map((element, index) => index + 20);
  for(let col of array){
     if(sheet.isColumnHiddenByUser(col)){
         sheet.showColumns(col);
         break;
     }
  }
}

上面的脚本一次只隐藏一列。我希望它一次隐藏两个。

google-apps-script menuitem
1个回答
0
投票

这样试试:

hideColumns(20,2);
© www.soinside.com 2019 - 2024. All rights reserved.