3

I have a sheet which tracks pupil progress against a set of objectives - pupil name across the top, objectives listed down the left hand side. Each pupil is in a group - in the example below the groups are indicated by SK, IO and NU.

enter image description here

I have created a sidebar with various filters.

enter image description here

The issue I am having is with the filter where the user will enter a group name, when the filter button is clicked all of the columns will hide except for the columns which contain the entered group name. I have the following code:

from the .gs sheet:

function showGroup(group) {
  for(var i=2; i<lastCol-36; i++) {
    if(data.getCell(8, i).getValue() != group) { 
    sheet.hideColumns(i);
    }
  }
} 

from the html sheet:

<form id="filterGroups" >
   <input type="text" value="" id="groupName">
   <input type="button" value="Filter"
     onclick="google.script.run.showGroup(filterGroups)" />

The closest I have got to success is to hide all columns providing the form id matches the parameter passed to google.script.run.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Glenn
  • 33
  • 2
  • In order to understand your situation, can you provide sample values for inputting to the text box of "Filters" and a sample spreadsheet? Because the script of ``showGroup(group)`` depends on the values which was inputted in "Filters". – Tanaike Aug 28 '18 at 22:20
  • Hello Tanaike, [HERE][1] is the sheet. Click on the red filters button in the top Left to show the sidebar. Is the user input field a user would enter, in this case, either SK, NU or IO. There corresponds to the groups as entered in Row 8 of the sheet 'Science Year Group'. When the user clicks the filter button beside the input field the columns hide except for the columns contains the value entered in the input field. Hope this makes sense. G [1]: https://docs.google.com/spreadsheets/d/1qMSN-4NRzzaEeKu0csBsi2R_Xs6-pdPw1Uk4OPBrUXo/edit?usp=sharing – Glenn Aug 29 '18 at 10:23
  • Thank you for replying. From your replying, I posted an answer. Could you please confirm it? – Tanaike Aug 30 '18 at 02:49
  • Did my answer show you the result what you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. If you have issues for my answer yet, feel free to tell me. I would like to study to solve your issues. – Tanaike Aug 31 '18 at 23:46

1 Answers1

0
  • You want to show only the columns with the values that user inputted. And you want to hide other columns except for them.
  • The values that user inputted are at row 8 of the sheet 'Science Year Group'.

If my understand of your question is correct, how about this modification?

Modification points:

  • If several strings (SK, NU) are put in the text box, if(data.getCell(8, i).getValue() != group){} is always false.
  • Give a name of name="groupName" to the text box. Using this, it retrieves the value using group.groupName at GAS side.
    • If the name is not given, you can retrieve the value using group[""].
  • I felt that when sheet.hideColumns(i) is used in the for loop, the process cost is high. So I used Sheets API for your situation.
    • By this, the hide and show of each column can be controlled by one API call.
    • At first, in order to use Sheets API, please enable Sheets API at Advanced Google Services and API console. You can see about how to enable Sheets API at here.

Modified scripts:

GAS
function showGroup(group) {
  var unhide = group.groupName.split(",").map(function(e){return e.trim().toUpperCase()});
  var values = sheet.getRange(8, 2, 1, (lastCol - 36) - 2).getValues()[0];
  var template = {updateDimensionProperties:{properties:{},range:{sheetId:sheet.getSheetId(),dimension:"COLUMNS"},fields:"hiddenByUser"}};
  var r = values.map(function(e, i) {
    var t = JSON.parse(JSON.stringify(template));
    if (~unhide.indexOf(e)) {
      t.updateDimensionProperties.properties.hiddenByUser = false;
    } else {
      t.updateDimensionProperties.properties.hiddenByUser = true;
    }
    t.updateDimensionProperties.range.startIndex = i + 1;
    t.updateDimensionProperties.range.endIndex = i + 2;
    return t;
  });
  Sheets.Spreadsheets.batchUpdate({"requests": r}, ss.getId());
}
HTML
<form id="filterGroups" >
  <input type="text" value="" id="groupName" name="groupName">
  <input type="button" value="Filter" onclick="google.script.run.showGroup(filterGroups)">
</form>

Note:

  • In this modified script, when SK, NU is put in the text box, only columns with SK, NU are shown. Other columns are hidden.

Reference:

If I misunderstand your question, please tell me. I would like to modify it.

Edit :

About Can you show me how to pass html form data to GAS functions? of your comment, I show you a sample script using your HTML form. The HTML form is as follows.

HTML

In this sample, the value is sent to showGroup(filterGroups) using google.script.run().

<form id="filterGroups" >
  <input type="text" value="" id="groupName" name="groupName">
  <input type="button" value="Filter" onclick="google.script.run.showGroup(filterGroups)">
</form>
GAS

In this sample, the value from HTML form is retrieved by group.groupName.

function showGroup(group) {
  Logger.log(group.groupName)
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Not sure what I am not doing but nothing is happening. As I am a novice can we do one thing at a time so I can understand what is happening. Can you show me how to pass html form data to GAS functions? Once I have that in my head I can look at using the API to make the script more efficient. – Glenn Aug 30 '18 at 09:44
  • @Glenn I'm really sorry for the inconvenience. About ``Can you show me how to pass html form data to GAS functions?``, I added a sample script. So could you please confirm it? When the sample works, by reflecting the script in my answer, you can control the hide and show of columns using the values from the form. Before run the script, please enable Sheets API at Advanced Google Services and API console. About how to enable it, please check my answer. – Tanaike Aug 30 '18 at 22:17
  • @Glenn If you cannot understand about added sample, can you share a sample spreadsheet including the sample script with the write permission? I would like to confirm it and modify it. I would like to resolve your issue. – Tanaike Aug 30 '18 at 22:19
  • I have got the script working with out the API using group.groupname. Will have another go at the API - I don't think I have enabled it although I did follow the instructions provided. Will have another go and update shortly. Thanks for the help so far! – Glenn Sep 02 '18 at 19:50
  • @Glenn Thank you for your response. – Tanaike Sep 03 '18 at 22:05