Possible to automatically collapse columns based on weeks (Monday being shown), and also only the column for today to be seen?

I’m quite new in the Apps Script world. I believe my question would come in 2 different/separate scripts:

  • As a new tab is created based on the same template, and the dates are being filled in, could the columns automatically be grouped (by script), with only the “Monday” of that week being visible? (Not sure if this is feasible, also I reckon not with protected ranges?
  • Could only the column for today be shown via a checkbox? (as could be found scripted on this post). Miserably failed at trying for this so far

Notes:

  • Columns A and B have to remain visible (frozen)
  • Rows 1 to 5 as well (frozen)
  • Current tab is called “September”, but more tabs (October etc) would need to have the same script being run

I tried the second point, but all my columns were just gone. I’m sure the problem lies with me lol

2

You have dates in columns; there is also a checkbox. If the status of the checkbox is:

  • checked then display the column for today’s date
  • not checked then display the column for Monday of this week.

You have unsuccessfully tried to apply the code from How to show columns based on today’s date, and hide the rest? (@Tanaike). The code in Google Sheets Apps Script to Find this weeks date for a day, as well as the follow weeks date for the same day was also helpful (@Kristkun).

Consider this script and the screenshots below (“today’s date” = 13/9/2024)

The key is in the syntax for show Columns – specifically the number of columns to show.

  • showColumns(columnIndex, numColumns)

In the referenced script, the data for one day consists of three columns (“Time In”, “Temp” and “Time Out” and the date is contained in the first column, so the script must display three columns:

  • sheet.showColumns(i + 3, 3)

In this case, the data for one day is a single column and this column also contains the date, so the script must display one column only.

  • sheet.showColumns(i + 3, 1)

function onEdit(e) {
  const { range } = e
  const sheet = range.getSheet()
  
  // checkbox is in B1
  if (range.getA1Notation() != "B1" || sheet.getSheetName() != "Sheet1") return

  // test if checkbox is checked (= "Today")
  if (!range.isChecked()) {
    // "Today" Checkbox is not checked, so display Monday
    // Logger.log("DEBUG: not checked, so display last Monday")

    // show all columns (unhide any columns)
    sheet.showColumns(3, sheet.getMaxColumns() - 2)
    // get the script timezone    
    var timeZone = Session.getScriptTimeZone()
    // Logger.log("DEBUG: timezone = "+timeZone)
    var curr = new Date()
    var mondayDate = curr.getDate() - curr.getDay()+1
    // Logger.log("DEBUG: curr = "+curr+", mondayDate = "+mondayDate+", curr get date = "+curr.getDate()+", day = "+(curr.getDay()+1)+", set date mondayDate = "+curr.setDate(mondayDate))
    var monday = Utilities.formatDate(new Date(curr.setDate(mondayDate)), timeZone, "E d MMM")
    // Logger.log("DEBUG: monday = "+monday)

    // hide all columns
    sheet.hideColumns(3, sheet.getMaxColumns() - 2)

    // show Monday only
    sheet.getRange(5, 3, 1, sheet.getLastColumn() - 2).getDisplayValues()[0]
    .forEach((e, i) => {
      if (e == monday) {
        sheet.showColumns(i + 3, 1)
      }
    })
    return
  }

  // "Today" Checkbox IS checked, so today's date
  // Logger.log("DEBUG: today = checked so display today")
  
  // get the script timezone    
  var timeZone = Session.getScriptTimeZone()
  // Logger.log("DEBUG: timezone = "+timeZone)
  
  // get the date
  var now = Utilities.formatDate(new Date(), timeZone, "E d MMM")
  // Logger.log("now = "+now)

  // hide all the columns
  sheet.hideColumns(3, sheet.getMaxColumns() - 2)
  // Logger.log("DEBUG: the data range = "+sheet.getRange(5, 3, 1, sheet.getLastColumn() - 2).getA1Notation())
  
  // show today's date
  sheet.getRange(5, 3, 1, sheet.getLastColumn() - 2).getDisplayValues()[0]
    .forEach((e, i) => {
      if (e == now) {
        sheet.showColumns(i + 3, 1);
      }
    })
}

Sample – Raw data


Sample – checked


Sample – unchecked

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật