我试图创建一个简单的切换按钮,该按钮每次单击时都会继续隐藏第5列以后的下一列。
例如
而且我想创建一个按钮,它也可以执行相反的操作。
例如
我有以下代码,我需要添加什么,以便每次单击/运行函数时它都继续显示/隐藏下一列?
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
}
逻辑是从列5
开始迭代列数。为此,您可以创建一个数组,其第一个元素5
到最后一个可用列(由getMaxColumns提供):
var array = Array(sheet.getMaxColumns()-4).fill().map((element, index) => index + 5);
如果单击该Hide Columns
按钮时遇到未隐藏的列,则只需将其隐藏即可。
如果单击该Show Columns
按钮并且遇到未隐藏的列,那么您将取消隐藏前一列。
要检查特定的列是否被隐藏,您需要使用isColumnHiddenByUser。
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;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句