简体   繁体   English

在四张纸上隐藏除今天以外的所有列(Google Spreadsheets脚本)

[英]Hide all but today's columns on four sheets (Google Spreadsheets script)

Goal: I'm trying to create a behavior tracker for four classes in Google Spreadsheets. 目标:我正在尝试为Google Spreadsheets中的四个类创建行为跟踪器。 The tracker has nine sheets: Class7A, Class7B, Class8A, Class8B, and Mon-Fri summary sheets. 跟踪器有九张纸:Class7A,Class7B,Class8A,Class8B和星期一至星期五摘要表。 The goal was for each ClassXX sheet to have behavior tracking information for an entire week, but for the default view to show only the current day's information. 我们的目标是让每个ClassXX工作表都具有整整一周的行为跟踪信息,而默认视图则仅显示当天的信息。

Attempts: During initial workup (with only the Class7A sheet created), I got this to work using a modification of the script found here (Thank you Jacob Jan Tuinstra!): Optimize Google Script for Hiding Columns 尝试:在最初的工作期间(仅创建了Class7A工作表),我使用此处找到的脚本的修改来使其工作(谢谢Jacob Jan Tuinstra!): 优化用于隐藏列的Google脚本

I modified it to check the value in the third row of each column (which held a 1 for Monday, 2 for Tuesday, etc), and if it did not match the numerical equivalent for the day of the week (var d = new Date(); var n = d.getDay();), then it would hide that column. 我对其进行了修改,以检查每列第三行中的值(星期一为1,星期二为2,依此类推),如果它与星期几的数值不匹配(var d = new Date) (); var n = d.getDay();),那么它将隐藏该列。 This process was somewhat slow - I'm assuming because of the iterating through each column - but it worked. 这个过程有点慢-我假设是因为遍历每一列-但它有效。

Quite excited, I went ahead and added the rest of the sheets, and tried again - but the code as written, seems to affect only the current sheet. 我很兴奋,我继续添加其余的工作表,然后再试一次-但是编写的代码似乎只影响当前的工作表。 I tried modifying it by replacing var sheet = ss.getSheets()[0]; 我尝试通过替换var sheet = ss.getSheets()[0];对其进行修改。 with for script that iterated through the columns, until i>4 (I've since lost that piece of code), with no luck. 对于用于遍历各列的脚本,直到i> 4(此后我丢失了那段代码),但没有运气。

Deciding to go back and try adapting the original version of the script to instead explicitly run multiple times for each named sheet, I found the that script no longer seems to work at all. 决定回去尝试修改脚本的原始版本以针对每个命名工作表显式运行多次,我发现该脚本似乎不再起作用。 I get various version of "cannot find XX function in sheet" or "cannot find XX function in Range." 我得到各种版本的“在工作表中找不到XX函数”或“在Range中找不到XX函数”。

Source: A shared version (with student info scrubbed) can be found here: https://docs.google.com/spreadsheets/d/1OMq4a4_Gh_xyNk_IRy-mwJn5Hq36RXmdAzTzx7dGii0/edit?usp=sharing (editing is on). 来源:可以在此处找到共享版本(已清除学生信息): https ://docs.google.com/spreadsheets/d/1OMq4a4_Gh_xyNk_IRy-mwJn5Hq36RXmdAzTzx7dGii0/edit?usp =sharing (正在启用编辑)。

Stretch Goal: Ultimately, I need to get this to reliably show only the current day's columns (either through preset ranges (same for each sheet), or the 1-5 values), and I need it to do so for all four ClassXX sheets, but not the summary pages (and preferably more quickly than the iterations). 延伸目标:最终,我需要使它可靠地仅显示当天的列(通过预设范围(每张纸相同)或1-5值),并且我需要对所有四张ClassXX纸都这样做,而不是摘要页面(最好比迭代页面更快)。 If necessary, I can remove the summary pages and set them up externally, but that's not my first preference. 如有必要,我可以删除摘要页面并在外部进行设置,但这不是我的首选。 I would deeply appreciate any help with this; 我对此深表感谢。 so far my attempts have seemed to only take me backwards. 到目前为止,我的尝试似乎只会使我倒退。

Thanks! 谢谢!

Current code: 当前代码:

function onOpen() {
  // get active spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // create menu
  var menu = [
    {name: "Show Today Only", functionName: "hideColumn"},
    {name: "Show All Days", functionName: "showColumn"},
    {name: "Clear Week - WARNING will delete all data", functionName: "clearWeek"}
  ];

  // add to menu
  ss.addMenu("Show Days", menu);
}
var d = new Date();
var n = d.getDay();


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(2, i).getValue() != n) {
        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);
}

I cannot recreate the problem of the script not working at all, it's working fine for Class7A so that part is working fine. 我无法重现脚本根本无法工作的问题,它对于Class7A正常工作,因此该部分工作正常。

So let's look at the two other problems: 因此,让我们看一下另外两个问题:

  1. Applying this to all Sheets 将此应用于所有工作表
  2. Speeding up the script 加快脚本

First let's create some globals we use in both functions 首先让我们创建两个函数中都使用的全局变量

var d = new Date();
var n = d.getDay();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = ss.getSheets().map(function(sheet) {return sheet.getName();});
var classSheets = sheetNames.filter(function(sheetName) {return sheetName.match("Class")});

Now we can iterate over classSheets and get the sheet by name and hide columns in each. 现在,我们可以遍历classSheets并按名称获取表单,并在每个表单中隐藏列。
However hiding each individual column is very slow. 但是,隐藏每个单独的列非常慢。
The sheet is built very structured, every week has 12 columns (except for friday which doesn't have the grey bar), so we can just calculate the ranges we want to hide. 该工作表的结构非常有条理,每周有12列(周五没有灰色栏的除外),因此我们可以计算要隐藏的范围。

function hideColumn() {
  classSheets.map(function(sheetName){
    var sheet = ss.getSheetByName(sheetName);

    if (n == 1) {
      // Hide everything after the first three columns + Monday
      sheet.hideColumns(3 + 11, 12 * 4); 
    } else if (n == 5) { 
      // Hide everything to the left except the leftmost three columns 
      sheet.hideColumns(3, 4 * 12);
    } else {
      // Hide everything left of the current day
      sheet.hideColumns(3, (n - 1) * 12);
      // Hide everything after the current day
      sheet.hideColumns(3 + n * 12, (5 - n) * 12 - 1);
    }
  });
}

Lastly we can shorten showColumn 最后我们可以缩短showColumn

function showColumn() {
  classSheets.map(function(sheetName){
    var sheet = ss.getSheetByName(sheetName);
    var lastCol = sheet.getLastColumn();
    sheet.showColumns(1, lastCol);
  });
}

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

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