简体   繁体   English

Google 表格:根据第 1 行中的日期隐藏列

[英]Google Sheets: Hiding Columns based on date in row 1

I have no experience with scripting in Excel or Google Sheets, so I'm trying to branch out a bit and see if there's a solution to my problem.我没有在 Excel 或 Google Sheets 中编写脚本的经验,所以我试图扩展一下,看看是否有解决我的问题的方法。 We use Google Sheets for a weekly calendar at our kitchen remodeling business.我们在厨房改造业务中使用 Google 表格作为每周日历。 We organize the weeks from left to right and list the jobs we're currently working on in those columns.我们从左到右组织几周,并在这些列中列出我们目前正在处理的工作。 I would like to automatically hide all columns that have a date older than 4 weeks, so when the sheet opens, we're not starting with a date from a year ago.我想自动隐藏日期超过 4 周的所有列,因此当工作表打开时,我们不会从一年前的日期开始。 I can hide these columns manually each week, but when I do need to go back and look at previous weeks, I'm forced to unhide all thosecolumns and then highlight all of the columns I want to rehide.我可以每周手动隐藏这些列,但是当我确实需要返回查看前几周时,我不得不取消隐藏所有这些列,然后突出显示我想要重新隐藏的所有列。 Having a script seems like the better solution.拥有脚本似乎是更好的解决方案。

Is there a way to have a script run every time the file is open so that we're always only displaying the previous 4 weeks and everything in the future?有没有办法在每次打开文件时运行脚本,以便我们始终只显示前 4 周和未来的所有内容? If so, would you be willing to help me understand how I might write that and get it working?如果是这样,您是否愿意帮助我了解如何编写并使其工作? Again, I'm a novice when it comes to anything beyond formulas, but very interested in learning more about the scripting capabilities.再说一次,当涉及到公式以外的任何事情时,我是一个新手,但对了解有关脚本功能的更多信息非常感兴趣。

Thank you!谢谢!

日历

With Apps Script via Tools->Script Editor , you could create a menu with an onOpen() function .通过Tools->Script Editor使用Apps 脚本,您可以创建一个带有onOpen()函数的菜单。 The function in the menu (eg hidePast ), would then need to check a given value in each column (to see what date that column refers to), and then flag it to be hidden or not.菜单中的函数(例如hidePast )然后需要检查每列中的给定值(以查看该列所指的日期),然后将其标记为隐藏与否。 The onOpen function, because it is a "simple trigger", cannot do anything that requires "authorization" (such as interacting with non-local Spreadsheet data), hence the intermediate method. onOpen函数,因为它是一个“简单的触发器”,不能做任何需要“授权”的事情(比如与非本地电子表格数据交互),因此是中间方法。 By creating a menu, you can make it easy for anyone using the spreadsheet to authorize and activate the function.通过创建菜单,您可以让任何使用电子表格的人轻松授权和激活该功能。

Example:例子:

/* @OnlyCurrentDoc */
function onOpen() {
    SpreadsheetApp.getActive().addMenu("Date Tools",
        [{name:"Hide Past", functionName:"hidePast"},
         {name:"Show All", functionName:"showAll"}]);
}
function showAll() {
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getActiveSheet();
    sheet.unhideColumn(sheet.getDataRange());
    ss.toast("All columns unhidden.");
}
function hidePast() {
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getActiveSheet();
    // Acquire the 1st row of all used columns as an array of arrays.
    var datelist = sheet.getSheetValues(1, 1, 1, sheet.getLastColumn());

    // Drop the hours, minutes, seconds, etc. from today.
    var now = new Date();
    var today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));

    // Inspect the datelist and compare to today. Start from the rightmost
    // column (assuming the dates are chronologically increasing).
    var col = datelist[0].length;
    while(--col >= 0) {
        var then = new Date(datelist[0][col]);
        if(then < today) {
            break;
        }
    }

    // Bounds check, and convert col into a 1-base index (instead of 0-base).
    if(++col < 1) return;

    // col now is the first index where the date is before today.
    // Increment again, as these are 2-column merged regions (and
    // the value is stored in the leftmost range). If not incremented,
    // (i.e. hiding only part of a merged range), spreadsheet errors will occur.
    sheet.hideColumn(sheet.getRange(1, 1, 1, ++col));
    ss.toast("Hid all the columns before today.");
}

Because you don't have a "database like" source it will be very difficult, but you can try to create a very complicated因为您没有“类似数据库”的源,所以会非常困难,但是您可以尝试创建一个非常复杂的

QUERY()

you should filter the dates in another sheet (and you may face a dead end).您应该在另一张纸中过滤日期(并且您可能会面临死胡同)。

So I will suggest using this kind of structure and it will also allow you to make other kinds of filters (or Pivot Tables) in the future (maintainable and scalable).所以我建议使用这种结构,它也允许你在未来制作其他类型的过滤器(或数据透视表)(可维护和可扩展)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM