所以我有一个电子表格,基本上是一个包含12个月约会的日记,我在电子表格中包含以下脚本隐藏列(以节省时间)问题是数据太多而且这需要一个很长一段时间,尤其是hideAll功能!
我想知道是否有一种方法可以更改这些功能以将列隐藏为范围而不是单独的列,到目前为止我还没有设法找到这样的内容。我需要能够设置相同的菜单,并且能够单独隐藏每个月,如果可能的话,也将所有功能隐藏起来(如现有脚本,但不是单独的块)。这必须是范围,因为我每个月之间的列需要保持可见而不是隐藏。
请参阅下面的示例电子表格(这不是最新版本,但它包含的脚本是真实版本的实时使用!)
https://docs.google.com/spreadsheets/d/1sOlQEzG1D29RaY86YeR1Da--c8t94J-ZAGjv52U4dsY/edit?usp=sharing
function onOpen(){
SpreadsheetApp.getUi().createMenu('Sophie\'s Menu :)')
.addItem('Relative validation', 'copyValidation')
.addItem('Relative rows, absolute columns', 'copyValidationColumnsAbsolute')
.addItem('Absolute rows, relative columns', 'copyValidationRowsAbsolute')
.addItem('Hide All Months', 'hideAll')
.addItem('Hide January', 'hideCols1')
.addItem('Hide February', 'hideCols2')
.addItem('Hide March', 'hideCols3')
.addItem('Hide April', 'hideCols4')
.addItem('Hide May', 'hideCols5')
.addItem('Hide June', 'hideCols6')
.addItem('Hide July', 'hideCols7')
.addItem('Hide August', 'hideCols8')
.addItem('Hide September', 'hideCols9')
.addItem('Hide October', 'hideCols10')
.addItem('Hide November', 'hideCols11')
.addItem('Hide December', 'hideCols12')
.addToUi();
}
function copyValidation(rowsAbsolute, columnsAbsolute)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var r = ss.getActiveRange();
var dv = r.getDataValidations();
var dvt = dv[0][0].getCriteriaType();
if (dvt != SpreadsheetApp.DataValidationCriteria.VALUE_IN_RANGE) return;
var dvv = dv[0][0].getCriteriaValues();
Logger.log(dvv);
for (var i = 0; i < dv.length; i++)
{
for (var j = i ? 0 : 1; j < dv[0].length; j++)
{
dv[i][j] = dv[0][0].copy().withCriteria(dvt, [dvv[0].offset(rowsAbsolute ?
0 : i, columnsAbsolute ? 0 : j), dvv[1]]).build();
}
}
r.setDataValidations(dv);
}
function copyValidationRowsAbsolute()
{
copyValidation(true, false);
}
function copyValidationColumnsAbsolute()
{
copyValidation(false, true);
}
function hideCols1(){
showhideCols('3,4,5,6,7,8,9,10,11,12,13,14,15,16,
17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33',false);
}
function hideCols2(){
showhideCols('35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62',false);
}
function hideCols3(){
showhideCols('64,65,66,67,68,69,70,71,72,73,74,75,76,77,
78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94',false);
}
function hideCols4(){
showhideCols('96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,
111,112,113,114,115,116,117,118,119,120,121,122,123,124,125',false);
}
//Right up to hideCols12
function hideCols12(){
showhideCols('348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,
364,365,366,367,368,369,370,371,372,373,374,375,376,377,378',false);
}
//I even have a hideAll function that contains every single column listed above!
function showhideCols(colscsv,show){
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
if(!colscsv){
var resp=SpreadsheetApp.getUi().prompt('Enter Column Numbers separated by
commas.', SpreadsheetApp.getUi().Button.OK);
var cA=resp.getResponseText().split(',');
}else{
var cA=colscsv.split(',');
}
for(var i=0;i<cA.length;i++){
if(show){
sh.showColumns(cA[i]);
}else{
sh.hideColumns(cA[i]);
}
}
}
答案 0 :(得分:0)
如果ALTER TABLE myTable ADD new_col DateTime2(3);
UPDATE myTable SET new_col = oldColName;
ALTER TABLE myTable DROP COLUMN oldColName;
EXEC sp_rename 'myTable.new_col', 'oldColName', 'COLUMN';
中的索引是静态的,那么您只需要将范围中的起始hideColsN()
和columnIndex
发送到hideColumns(columnIndex, numColumns)
numColumns
function hideCols1(){
showhideCols(3, 31, false);
}
编辑:我可能会忽视它,但我没有看到您的function showhideCols(start, amount, show){
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
// set variables to the list of numbers from the prompt
// may want to change it to take the startIndex and number of columns to hide/show.
// also there was not a way listed to determine if the columns should be hidden or not.
if(!start){
var resp=SpreadsheetApp.getUi().prompt('Enter Column Numbers separated by commas.', SpreadsheetApp.getUi().ButtonSet.OK);
var cA=resp.getResponseText().split(',');
var start = cA[0];
var amount = cA.length;
var show = false;
}
// Show or hide column ranges in one call
if(show){
sh.showColumns(start, amount);
}else{
sh.hideColumns(start, amount);
}
}
方法,因此未添加到此答案中