隐藏和取消隐藏列切换按钮 [英] Hiding and unhiding columns toggle button

查看:72
本文介绍了隐藏和取消隐藏列切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的切换按钮,该按钮每次单击时都会继续隐藏第5列的下一列.

例如

  1. 单击按钮以隐藏6/F列
  2. 再次单击按钮以隐藏第7/G列
  3. 再次单击按钮以隐藏第8/H列

我想创建一个按钮,该按钮也可以执行相反的操作.

例如

  1. 单击按钮取消隐藏6/F列
  2. 再次单击按钮可取消隐藏7/G

我有下面的代码,我需要添加什么,以便每次单击/运行函数时它都继续显示/隐藏下一列?

 函数hideCol(){var sheet = SpreadsheetApp.getActive().getSheetByName('testsheet');//将工作表的名称替换为"Main"sheet.hideColumns(6);//F}函数onOpen(){var ui = SpreadsheetApp.getUi();ui.createMenu('自定义菜单').addItem('隐藏列','hideCol').addItem("Show Columns",'show').addToUi();}函数show(){var sheet = SpreadsheetApp.getActive().getSheetByName('testsheet');//将工作表的名称替换为"Main"sheet.showColumns(6);//F} 

解决方案

说明:

  • 逻辑是从 5 列开始迭代列数.为此,您可以创建一个数组,其第一个元素为 5 ,直到最后一个可用列,由 isColumnHiddenByUser .

解决方案:

  function onOpen(){var ui = SpreadsheetApp.getUi();ui.createMenu('自定义菜单').addItem('隐藏列','hideCol').addItem("Show Columns",'show').addToUi();}函数hideCol(){const sheet = SpreadsheetApp.getActive().getSheetByName('testsheet');//将工作表的名称替换为"Main"const array = Array(sheet.getMaxColumns()-4).fill().map((element,index)=> index + 5);for(让列的col){if(!sheet.isColumnHiddenByUser(col)){sheet.hideColumns(col);休息;}}}函数show(){const sheet = SpreadsheetApp.getActive().getSheetByName('testsheet');//将工作表的名称替换为"Main"const array = Array(sheet.getMaxColumns()-4).fill().map((element,index)=> index + 5);for(让列的col){if(sheet.isColumnHiddenByUser(col)){sheet.showColumns(col);休息;}}} 

I am trying to create a simple toggle button which continues to hide the next column from column 5 onwards every time it is clicked.

e.g.

  1. click button to hide column 6/F
  2. click button again to hide column 7/G
  3. click button again to hide column 8/H
  4. etc

And i want to create a button which does the reverse as well.

e.g.

  1. click button to unhide column 6/F
  2. click button again to unhide 7/G
  3. etc

I have the below code, what do I need to add so it continues to show/hide the next column every time I click/run the function?

function hideCol()
{
  var sheet = SpreadsheetApp.getActive().getSheetByName('testsheet'); //put the name of the sheet in place of 'Main'
  
  sheet.hideColumns(6); //F

}

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

function show() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('testsheet'); //put the name of the sheet in place of 'Main'
  sheet.showColumns(6);  //F

}

解决方案

Explanation:

  • The logic would be to iterate the number of columns starting from column 5 onwards. To do this, you can create an array for which the first element is 5 up to the last available column, provided by getMaxColumns:

    var array =  Array(sheet.getMaxColumns()-4).fill().map((element, index) => index + 5);
    

  • If you click on the Hide Columns button and you encounter an unhidden column then you simply hide it.

  • If you click on the Show Columns button and you encounter a unhidden column then you unhide the previous one.

  • To check whether a particular column is hidden or not, you need to use isColumnHiddenByUser.

Solution:

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

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

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

这篇关于隐藏和取消隐藏列切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆