0

I would like to create hyperlinks(have a list of names on first tab/sheet. Each name when clicked should go to a filter view with records in another sheet). Example: Clicking on "John Smith" in sheet 1 should take me to a filtered view with all the rows that have John Smith in sheet 2.

Example tables Sheet 1 table has names

Names
John Smith
Ryan Jones
Tony Welsh

Sheet 2 has sales info

Sales_id    Name
1245    John Smith
1234    John Smith
1256    Tony Welsh
5674    Ryan Jones
2345    Tony Welsh
1557    John Smith
9830    Ryan Jones

I want a filtered view for each name. So John Smith should have a filtered view of 3 records from sheet 2 ( sales:ids: 1245,1234 and 1557). These filtered views will have URLS that I would hyperlink in sheet 1 for each persons name

The code I have so far only creates a new Filterview for "John Smith" I would

  1. like to provide a list of names(not just limit to John Smith) to create multiple filter views.
  2. And for each filter view I want a the created link(fvid) so that I can use it as a hyperlink for each name on the first tab

Please help, I'm new to apps script :(

function filter_view (){
  const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet();
 
  const sheetId = spreadsheetId.getActiveSheet().getSheetId();
  
  const addFilterViewRequest = [
    {
       'addFilterView' : {
         filter : {
           title : 'John Smith', // Title Of filter view & make sure no space in title
           range : {
             sheetId : sheetId, // The filter view sheetId
             'startRowIndex': 0,
             'startColumnIndex':0,
             
           }, // Using comma to seprate different conditional values
            'criteria': {
              1:{  // This number the column you are indexing in the spreadsheet 
               'condition': {
                 'type': "TEXT_EQ",
                 'values':[
                   {
                     "userEnteredValue" :'John Smith'                      
                     
                   },

                 ],
               }
               
             }
           
         }

       }
    }
    }
  ]
    


   Sheets.Spreadsheets.batchUpdate({ requests: addFilterViewRequest },spreadsheetId.getId());
   // update the spreadsheet using the addFilterViewRequest 
    
}

Update 1: Figured out how to pass an array to get different filter views. I am able to print the fvid in the console. But i need to get the fvid(or the full url or filter view) in sheet 1. Here's the updated code



function create_filter_view (){
  var list_names = ["John Smith","Ryan Jones","Tony Welsh"];//Add names you want views for
  for(var i = 0; i < list_names.length; i++){
          
      const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet();
    
      const sheetId = spreadsheetId.getActiveSheet().getSheetId();
      
      const addFilterViewRequest = [
        {
          'addFilterView' : {
            filter : {
              title : list_names[i], // Title Of filter view & make sure no space in title
              range : {
                sheetId : sheetId, // The filter view sheetId
                'startRowIndex': 0,
                'startColumnIndex':0,
                
              }, // Using comma to seprate different conditional values
                'criteria': {
                  1:{  // This number the column you are indexing in the spreadsheet 
                  'condition': {
                    'type': "TEXT_EQ",
                    'values':[
                      {
                        "userEnteredValue" :list_names[i]                      
                        
                      },

                    ],
                  }
                  
                }
              
            }

          }
        }
        }
      ]
        


      response = Sheets.Spreadsheets.batchUpdate({ requests: addFilterViewRequest },spreadsheetId.getId());
      // update the spreadsheet using the addFilterViewRequest 
      filter_view_id = response['replies'][0]['addFilterView']['filter']['filterViewId']
      console.log(filter_view_id);
      

      
}

    
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. Can I ask you about the detail of your current issue and your goal? And also, I cannot understand `Example: Clicking on "John Smith" should take me to a filtered view with all the rows that have John Smith.`. Can I ask you about the detail of it? – Tanaike Jan 08 '22 at 00:37
  • Thank you for replying. I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I would be grateful if you can forgive my poor English skill. – Tanaike Jan 08 '22 at 01:26
  • Thank you for replying and adding more information. I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. For example, I cannot understand `Clicking on "John Smith"` of `Example: Clicking on "John Smith" should take me to a filtered view with all the rows that have John Smith.`. – Tanaike Jan 08 '22 at 03:21
  • And, I cannot understand `I want a filtered view for each name. So John Smith should have a filtered view of 3 records from sheet 2 ( sales:ids: 1245,1234 and 1557). These filtered views will have URLS that I would hyperlink in sheet 1 for each persons name`. For example, can you provide the sample input and output situations you expect as the image? By this, I would like to try to understand your question. I deeply apologize for my poor English skill, again. – Tanaike Jan 08 '22 at 03:21
  • @Tanaike: Please see Update 1 in my comment. I was able to get the filter view id in the console , but i need it in sheet 1(so that I can create hyperlinks). Does this make sense? – user17243359 Jan 08 '22 at 04:23
  • Thank you for replying and adding more information. From your replying, I proposed an answer. Could you please confirm it? If I misunderstood your question and that was not useful, I apologize. – Tanaike Jan 08 '22 at 06:10
  • @Tanaike Thanks for you quick response, this is what i was looking for. some follow up questions I have are. 1) Can I get some code to delete the filter views for just the names in Sheet 1(I don't want to delete all, just want to delete where the name matches). 2) Is it possible to look for name matches in Sheet 2 and sheet 3 and sheet 4(Imagine there is only one sheet with names- Sheet 1. But the sales data can be in Sheets 2, 3 ,4 . How can I modify my code to cycle through sheets 2 , 3 and 4 for the hyperlinks?) – user17243359 Jan 08 '22 at 06:41
  • @Tanaike: Also when I delete the filterviews, how do i clear the hyperlinks from the column in sheet1. It still has hyperlinks after i delete the filterviews – user17243359 Jan 08 '22 at 06:52
  • @Tanaike: I've added on the question. Please see link here: https://stackoverflow.com/questions/70634411/hyperlinks-of-multiple-filtered-views-in-google-sheets-using-apps-script – user17243359 Jan 08 '22 at 16:47
  • @Tanaike: I also have a different question that I was struggling with , no one else was able to give a solution : https://stackoverflow.com/questions/69711440/batch-update-bigquery-results-into-google-sheetsabout-50k-results – user17243359 Jan 08 '22 at 16:50

1 Answers1

1

I believe your goal is as follows.

  • You have 2 sheets of "Sheet1" and "Sheet2". The cells of "Sheet1" and "Sheet2" are shown in your question.
  • You want to create the filter views to the column "B" of "Sheet2" using the values from the column "A" of "Sheet1".
  • The cells of column "A" have the hyperlink of the filter view. When a cell is clicked, you want to jump to the filter view corresponding to the cell value.

In this case, how about the following sample script?

Modification points:

  • It seems that the property of criteria is deprecated. So in this case, please use filterSpecs[]. Ref
  • In order to set the hyperlink of the filter view, RichTextValue is used.

Sample script:

Before you use this script, please enable Sheets API at Advanced Google services.

function create_filter_view() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var sheet1 = ss.getSheetByName("Sheet1");
  var sheet2 = ss.getSheetByName("Sheet2");
  var sheetId2 = sheet2.getSheetId();
  var range1 = sheet1.getRange("A2:A" + sheet1.getLastRow());
  var values1 = range1.getValues();
  var requests = values1.map(([a]) => ({ addFilterView: { filter: { title: a, range: { sheetId: sheetId2, startRowIndex: 0, startColumnIndex: 0 }, filterSpecs: [{ columnIndex: 1, filterCriteria: { condition: { type: "TEXT_EQ", values: [{ userEnteredValue: a }] } } }] } } }));
  var response = Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
  var filter_view_ids = response.replies.map(({ addFilterView: { filter: { filterViewId } } }) => filterViewId);
  var richTextValues = filter_view_ids.map((e, i) => [SpreadsheetApp.newRichTextValue().setText(values1[i][0]).setLinkUrl(`#gid=${sheetId2}&fvid=${e}`).build()]);
  range1.setRichTextValues(richTextValues);
}
  • When this script is run, the values are retrieved from the column "A" of "Sheet1", and the filter views are created to "Sheet2" using the retrieved values. In this case, column "B" is filtered. And then, the links of filter views are set to the column "A" of "Sheet1".

Note:

  • When the above script is run 2 times, the same filter views are created. So, when you want to reset the filter views (delete all filter views), you can also use the following script.

      function deleteAllFinterViews() {
        var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
        var ids = Sheets.Spreadsheets.get(ssId).sheets.flatMap(s => s.filterViews ? s.filterViews.map(e => ({ deleteFilterView: { filterId: e.filterViewId } })) : []);
        if (ids.length > 0) Sheets.Spreadsheets.batchUpdate({ requests: ids }, ssId);
      }
    
  • This sample script is for your sample Spreadsheet showing in your script. So when the structure of your actual Spreadsheet is different from your sample one, the script might not be able to be used. Please be careful about this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This is awesome, It is exactly what I need. Thank you so much. some follow up questions I have are. 1) Can I get some code to delete the filter views for just the names in Sheet 1(I don't want to delete all, just want to delete where the name matches). 2) Is it possible to look for name matches in Sheet 2 and sheet 3 and sheet 4(Imagine there is only only sheet with names- Sheet 1. But the sales data can be in Sheets 2, 3 ,4 . How can I modify my code to cycle through sheets 2 , 3 and 4 for the hyperlinks?) – user17243359 Jan 08 '22 at 06:28
  • @user17243359 Thank you for replying. I'm glad your issue was resolved. I would like to support you. But the issue of replying is new issue, and that is different from your question. So can you post it as new question? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as new question, users including me can think of it. If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike Jan 08 '22 at 08:08
  • https://stackoverflow.com/questions/70634411/hyperlinks-of-multiple-filtered-views-in-google-sheets-using-apps-script – user17243359 Jan 08 '22 at 16:46
  • Thanks. I have added the new question here:https://stackoverflow.com/questions/70634411/hyperlinks-of-multiple-filtered-views-in-google-sheets-using-apps-script – user17243359 Jan 08 '22 at 16:52
  • I also have a different question that I was struggling with , no one else was able to give a solution : https://stackoverflow.com/questions/69711440/batch-update-bigquery-results-into-google-sheetsabout-50k-results – user17243359 Jan 08 '22 at 16:53