亲爱的精彩脚本,
抱歉不知道所有的术语,我充其量只是一个新手,但我已经碰壁了。我试图隐藏/显示基于不同单元格中的两个值的列。
我有一张名为" Start"其值在O2中变化(年)。这对应于名为"主题" (见图)。受试者有一个标题为"准备,1 - 2年,3 - 4年和5 - 6年和#34;的科目和专栏列表。我想编写一个检查Start上的值的解决方案,然后读取Subjects上1s和0s的列,并在名为" Reports"。
的另一个工作表上显示/隐藏列。例如,如果开始时的O2表示1-2年,则脚本会查看主题(D1),然后在与这些主题对应的报表上显示或隐藏(1或0)列(例如Preps on IPDIP so so如果值为Year 1-2,那么Reports上的列将被隐藏。Reports上有多个列将被隐藏或显示。
我目前正在使用此脚本:
function ShowHideSubjects() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Reports");
sheet.showColumns(1, sheet.getMaxColumns())
var condition = sheet.getRange("Subjects!C2").getValue();
if (condition = 1) {
sheet.showColumns(10,1)
}
var condition = sheet.getRange("Subjects!C2").getValue();
if (condition = 0) {
sheet.hideColumns(10,1)
}
}
这里有一些要解释的照片。
[开始时的价值] [1]
非常感谢您的帮助。这张Google电子表格目前每年为我们的学校节省1000美元,这样做很棒。
和平与感谢,
韦恩
答案 0 :(得分:1)
function onOpen()
{
SpreadsheetApp.getUi().createMenu('My Tools')
.addItem('ShowHideSubjects','ShowHideSubjects')
.addToUi();
}
function ShowHideSubjects()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var reportsSheet = ss.getSheetByName('Reports');
var subjectsSheet = ss.getSheetByName('Subjects')
var startSheet = ss.getSheetByName('Start');
var startSubjectsRange = startSheet.getRange('O2');
var reportsSheetDataRange = reportsSheet.getDataRange();
var reportsSheetDataArray = reportsSheetDataRange.getValues();
var reportsSheetDataRangeLastRow = reportsSheetDataRange.getLastRow();
var reportsSheetDataRangeLastColumn = reportsSheetDataRange.getLastColumn();
for(var i = 0;i < reportsSheetDataRangeLastRow;i++)
{
for(var j = 0;j < reportsSheetDataRangeLastColumn;j ++)
{
if(reportsSheetDataArray[i][j] == 'IPDIP')
{
var ipdipRow = i + 1;
var ipdipCol = j + 1;
break;
}
}
}
var reportsNumColumns = reportsSheetDataRange.getLastColumn() - ipdipCol + 1
//check that its the right reports range
//reportsSheet.setActiveSelection(reportsSheet.getRange(ipdipRow, ipdipCol, 1 , reportsNumColumns));
var reportsHeaderRange = reportsSheet.getRange(ipdipRow, ipdipCol, 1 , reportsNumColumns);
var subjectsSheetDataRange = subjectsSheet.getDataRange();
var subjectsSheetDataArray = subjectsSheetDataRange.getValues();
var subjectsSheetDataRangeLastRow = subjectsSheetDataRange.getLastRow();
var subjectsSheetDataRangeLastColumn = subjectsSheetDataRange.getLastColumn();
for(var i = 0;i < subjectsSheetDataRangeLastRow;i++)
{
for(var j = 0;j < subjectsSheetDataRangeLastColumn;j ++)
{
if(subjectsSheetDataArray[i][j] == 'Subjects')
{
var subjectsRow = i + 1;
var subjectsCol = j + 1;
break;
}
}
}
//check that its the right subjects range
//subjectsSheet.setActiveSelection(subjectsSheet.getRange(subjectsRow, subjectsCol,subjectsSheetDataRange.getLastRow() - subjectsRow + 1 ,subjectsSheetDataRange.getLastColumn() - subjectsCol + 1));
var subjectsShowHideHeaderRange = subjectsSheet.getRange(subjectsRow, subjectsCol, 1 ,subjectsSheetDataRange.getLastColumn() - subjectsCol + 1);
var subjectsShowHideHeaderArray = subjectsShowHideHeaderRange.getValues();
var subjectsShowHideRange = subjectsSheet.getRange(subjectsRow, subjectsCol,subjectsSheetDataRange.getLastRow() - subjectsRow + 1 ,subjectsSheetDataRange.getLastColumn() - subjectsCol + 1);
//checked subjectsShowHideRange
//subjectsSheet.setActiveSelection(subjectsShowHideRange);
var w = subjectsShowHideHeaderRange.getWidth();
var startString = startSubjectsRange.getValue();
for(var i = 0;i < w;i++)
{
if(subjectsShowHideHeaderArray[0][i] == startString)
{
var subjectsShowHideColumn = subjectsCol + i;
break;
}
}
//checked the subjectsShowHideColumn
//SpreadsheetApp.getUi().alert( startSubjectsRange.getValue() + ' is column ' + subjectsShowHideColumn + ' on the subjects page. And subjects is on column ' + subjectsCol + '.');
//Checked the subjectsShowHideRange
//subjectsSheet.setActiveSelection(subjectsShowHideRange);
var subjectsShowHideArray = subjectsShowHideRange.getValues();
var subjectsShowHideRangeHeight = subjectsShowHideRange.getHeight();
//for(var i = 0;i < subjectsShowHideRangeHeight;i++)
for(var i = subjectsShowHideRangeHeight -1;i >= 0;i--)
{
var subject = subjectsShowHideArray[i][0];
var show = subjectsShowHideArray[i][subjectsShowHideColumn-subjectsCol];
var showhidecolsS=reportsColumnHeaderMatch(ipdipRow,ipdipCol,reportsNumColumns,subject);
if(showhidecolsS)
{
var showhidecolsA=showhidecolsS.split(',');
if(showhidecolsA)
{
for(var j = 0; j < showhidecolsA.length; j++)
{
columnsShowHide(showhidecolsA[j],show);
}
}
}
}
}
function columnsShowHide(column, show)
{
var column = (typeof(column) !== 'undefined')? column: 0;
var show = (typeof(show) !== 'undefined')? show: 1;
if(column == 0)
{
SpreadsheetApp.getUi().alert('Error: The column parameter passed to columnsShowHide is undefined.');
return;
}
else
{
switch(show)
{
case 1:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').showColumns(column);
//SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').getRange(1,column,10,1).setBackground('yellow');
break;
case 0:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').hideColumns(column);
//SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').getRange(1,column,10,1).setBackground('white');
break;
default:
SpreadsheetApp.getUi().alert('Show parameter is out of range not 0 or 1 in function columnsShowHide');
}
}
}
function reportsColumnHeaderMatch(row, col, numcolumns, subject)
{
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports');
var rng = sht.getRange(row, col, 1, numcolumns);
var rngA = rng.getValues();
var result = '';
var firsttime = true;
for(var i = 0;i < numcolumns;i++)
{
//var s = rngA[0][i].toString();
var s = rngA[0][i];
var re = new RegExp('^' + subject);
if(s.match(re))
{
if(!firsttime)result += ',';
result += col + i;
firsttime = false;
}
}
return result;
}
function dispStatus(title,html,width,height)
{
// Display a modeless dialog box with custom HtmlService content.
var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
var width = typeof(width) !== 'undefined' ? width : 250;
var height = typeof(height) !== 'undefined' ? height : 300;
var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(width)
.setHeight(height);
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
}
我有一个工作版本,但我遇到了问题。我正在使用正则表达式RegExp(&#39; ^&#39; + subject)。这允许我做的是动态构建正则表达式。当主题是IPDIP时,正则表达式^ IPDIP匹配IPDIP列标题的所有字符,并且因为第1-2年的第二行是0,所以列IPDIP被关闭但是接下来的主题是IPD和正则表达式^ IPD也匹配IPDIP,而第1-2年的第三行现在关闭了IPDIP,这可能是一个问题。我计划使用这种技术来匹配其余的列,因为主题只是列标题其余部分的前三个字母,这将是捕获它们的好方法。所以我想问题是你想要在1-2年报告中打开或关闭IPDIP。
哦,我只是想到所有Reports列标题名称都是六个字符或更大字符。我可以利用它,因为IPDIP只有5个字符。所以我只能将正则表达式应用于6个字符或更大的标题。但我需要你的意见,因为我猜测实际的报告表可能比我看到的更广泛。
哦,我想另一个解决方案可能是从主题栏的底部到顶部,然后IPDIP将始终是5个字符匹配。
顺便说一下,我非常感谢能够帮助你。我的妻子是一名教师,我知道你最近必须走出学生人性的海洋。所以,如果有任何我可以帮忙的事情,请随时告诉我。
我刚才注意到我们在ICT和ICTICT方面遇到了同样的问题。
现在我决定从下到上颠倒从主题栏开始的顺序。只要你总是在顶部放置重叠匹配最长的字符串就可以了。
我对此代码所做的一件事是使表格的来源成为主题页面上主题标题的位置和“报告”页面上的IPDIP标题。因此,您可以移动表格(即向左侧和当前数据上方添加列)。该程序计算出数据位于这两个关键位置的位置,并自动重新调整。另一种说法就是说它并不假设你表中的第一行/列是行/列1 / A
答案 1 :(得分:0)
完全按照要求工作,首先尝试:-)感谢Cooper的辛勤工作。
function onOpen()
{
SpreadsheetApp.getUi().createMenu('My Tools')
.addItem('ShowHideSubjects','ShowHideSubjects')
.addToUi();
}
function ShowHideSubjects()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var reportsSheet = ss.getSheetByName('Reports');
var subjectsSheet = ss.getSheetByName('Subjects')
var startSheet = ss.getSheetByName('Start');
var startSubjectsRange = startSheet.getRange('O2');
var reportsSheetDataRange = reportsSheet.getDataRange();
var reportsSheetDataArray = reportsSheetDataRange.getValues();
var reportsSheetDataRangeLastRow = reportsSheetDataRange.getLastRow();
var reportsSheetDataRangeLastColumn = reportsSheetDataRange.getLastColumn();
for(var i = 0;i < reportsSheetDataRangeLastRow;i++)
{
for(var j = 0;j < reportsSheetDataRangeLastColumn;j ++)
{
if(reportsSheetDataArray[i][j] == 'IPDIP')
{
var ipdipRow = i + 1;
var ipdipCol = j + 1;
break;
}
}
}
var reportsNumColumns = reportsSheetDataRange.getLastColumn() - ipdipCol + 1
//check that its the right reports range
//reportsSheet.setActiveSelection(reportsSheet.getRange(ipdipRow, ipdipCol, 1 , reportsNumColumns));
var reportsHeaderRange = reportsSheet.getRange(ipdipRow, ipdipCol, 1 , reportsNumColumns);
var subjectsSheetDataRange = subjectsSheet.getDataRange();
var subjectsSheetDataArray = subjectsSheetDataRange.getValues();
var subjectsSheetDataRangeLastRow = subjectsSheetDataRange.getLastRow();
var subjectsSheetDataRangeLastColumn = subjectsSheetDataRange.getLastColumn();
for(var i = 0;i < subjectsSheetDataRangeLastRow;i++)
{
for(var j = 0;j < subjectsSheetDataRangeLastColumn;j ++)
{
if(subjectsSheetDataArray[i][j] == 'Subjects')
{
var subjectsRow = i + 1;
var subjectsCol = j + 1;
break;
}
}
}
//check that its the right subjects range
//subjectsSheet.setActiveSelection(subjectsSheet.getRange(subjectsRow, subjectsCol,subjectsSheetDataRange.getLastRow() - subjectsRow + 1 ,subjectsSheetDataRange.getLastColumn() - subjectsCol + 1));
var subjectsShowHideHeaderRange = subjectsSheet.getRange(subjectsRow, subjectsCol, 1 ,subjectsSheetDataRange.getLastColumn() - subjectsCol + 1);
var subjectsShowHideHeaderArray = subjectsShowHideHeaderRange.getValues();
var subjectsShowHideRange = subjectsSheet.getRange(subjectsRow, subjectsCol,subjectsSheetDataRange.getLastRow() - subjectsRow + 1 ,subjectsSheetDataRange.getLastColumn() - subjectsCol + 1);
//checked subjectsShowHideRange
//subjectsSheet.setActiveSelection(subjectsShowHideRange);
var w = subjectsShowHideHeaderRange.getWidth();
var startString = startSubjectsRange.getValue();
for(var i = 0;i < w;i++)
{
if(subjectsShowHideHeaderArray[0][i] == startString)
{
var subjectsShowHideColumn = subjectsCol + i;
break;
}
}
//checked the subjectsShowHideColumn
//SpreadsheetApp.getUi().alert( startSubjectsRange.getValue() + ' is column ' + subjectsShowHideColumn + ' on the subjects page. And subjects is on column ' + subjectsCol + '.');
//Checked the subjectsShowHideRange
//subjectsSheet.setActiveSelection(subjectsShowHideRange);
var subjectsShowHideArray = subjectsShowHideRange.getValues();
var subjectsShowHideRangeHeight = subjectsShowHideRange.getHeight();
//for(var i = 0;i < subjectsShowHideRangeHeight;i++)
for(var i = subjectsShowHideRangeHeight -1;i >= 0;i--)
{
var subject = subjectsShowHideArray[i][0];
var show = subjectsShowHideArray[i][subjectsShowHideColumn-subjectsCol];
var showhidecolsS=reportsColumnHeaderMatch(ipdipRow,ipdipCol,reportsNumColumns,subject);
if(showhidecolsS)
{
var showhidecolsA=showhidecolsS.split(',');
if(showhidecolsA)
{
for(var j = 0; j < showhidecolsA.length; j++)
{
columnsShowHide(showhidecolsA[j],show);
}
}
}
}
}
function columnsShowHide(column, show)
{
var column = (typeof(column) !== 'undefined')? column: 0;
var show = (typeof(show) !== 'undefined')? show: 1;
if(column == 0)
{
SpreadsheetApp.getUi().alert('Error: The column parameter passed to columnsShowHide is undefined.');
return;
}
else
{
switch(show)
{
case 1:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').showColumns(column);
//SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').getRange(1,column,10,1).setBackground('yellow');
break;
case 0:
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').hideColumns(column);
//SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports').getRange(1,column,10,1).setBackground('white');
break;
default:
SpreadsheetApp.getUi().alert('Show parameter is out of range not 0 or 1 in function columnsShowHide');
}
}
}
function reportsColumnHeaderMatch(row, col, numcolumns, subject)
{
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reports');
var rng = sht.getRange(row, col, 1, numcolumns);
var rngA = rng.getValues();
var result = '';
var firsttime = true;
for(var i = 0;i < numcolumns;i++)
{
//var s = rngA[0][i].toString();
var s = rngA[0][i];
var re = new RegExp('^' + subject);
if(s.match(re))
{
if(!firsttime)result += ',';
result += col + i;
firsttime = false;
}
}
return result;
}
function dispStatus(title,html,width,height)
{
// Display a modeless dialog box with custom HtmlService content.
var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
var width = typeof(width) !== 'undefined' ? width : 250;
var height = typeof(height) !== 'undefined' ? height : 300;
var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(width)
.setHeight(height);
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
}