问题描述
在我的Google电子表格A中,我结合使用TRANSPOSE和IMPORTRANGE公式从日历电子表格B中导入数据,以便填写事件的工作时间表.因为在每个日期都有3个事件槽,它们并不总是被填充,所以我得到了很多过时的列.
In my google spreadsheet A, I use a combination of the TRANSPOSE and IMPORTRANGE formulas to import data from calendar spreadsheet B, in order to fill out a working schedule for events. Because on each date there are 3 event slots, which are not always filled, I am getting a lot of obsolete columns.
表布局:
row1: 01-01-2013 01-01-2013 01-01-2013 02-01-2013
row2: Event_ID Event_ID Event_ID Event_ID
row3: Event_name Event_name Event_name Event_name
第1行和第2行包含自动生成的日期和event_ID,因此它们永远不会为空.手机号码.当没有事件添加到该插槽时,3会显示为空,但实际上其中存在一个CONTINUE公式以继续从单元格A1导入importrange公式.
Rows 1 and 2 contain auto-generated dates and event_ID's so these are never empty. Cell nr. 3 displays empty when there was no event added to that slot, but in fact there is a CONTINUE-formula in there to continue the importrange-formula from cell A1.
我正在寻找一个脚本来自动隐藏nr单元格中的列. 3不包含导入的数据.
I´m looking for a script to automatically hide the colums in which cell nr. 3 doesn't contain imported data.
不了解JavaScript(但愿意学习),我试图将现有脚本中的代码组合在一起,但目前我无法理解这些代码...
Not understanding a thing about JavaScript (but willing to learn), I have tried to combine pieces from existing scripts but at this point I cannot manage to make any sense of these codes...
推荐答案
以下代码可以解决问题:
The following code does the trick:
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [{name: "Hide columns", functionName: "hideColumn"},
{name: "Show all columns", functionName: "showColumn"}];
// add to menu
ss.addMenu("Check", menu);
}
function hideColumn() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(3, i).getValue() == '') {
sheet.hideColumns(i);
}
}
}
function showColumn() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn();
// show all columns
sheet.showColumns(1, lastCol);
}
- 打开文件后创建菜单
- 第一个菜单选项,隐藏第3行为空的所有列
- 第二个选项,显示所有隐藏的列
请参阅示例文件,我准备对其进行操作:根据单元格值隐藏列
See example file I prepared to watch it in action: Hide columns based on cell value
这篇关于根据单元格值隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!