Subhendu
Subhendu

Reputation: 49

Google apps script, get values from row(s) based on a specific cell value

I would like to display data from the whole row where Username matches [email protected]. My google sheet looks like below.

Timestamp           Username        from        to
24/03/2015 22:21:20 [email protected]   17/03/2015  24/03/2015
24/03/2015 22:21:20 [email protected]   18/03/2015  26/03/2015
24/03/2015 22:21:20 [email protected]   16/03/2015  26/03/2015
24/03/2015 22:21:20 [email protected]   03/03/2015  04/04/2015
24/03/2015 22:21:20 [email protected]   04/03/2015  18/03/2015

I want to display the data like the below using apps script.

Timestamp           Username        from         to
24/03/2015 22:21:20 [email protected]   17/03/2015  24/03/2015
24/03/2015 22:21:20 [email protected]   16/03/2015  26/03/2015
24/03/2015 22:21:20 [email protected]   03/03/2015  04/04/2015

To display the result can I use this?

  var app = UiApp.createApplication();
  app.add(app.createHTML('data from table'))
  return app

Please help. Thank you.

Upvotes: 1

Views: 4666

Answers (1)

Dan Oswalt
Dan Oswalt

Reputation: 2189

UIApp service is deprecated, you would use HTML service for something similar, but, if all you want is to display the info for that email address, you could try this script:

This will open up a simple message box like an alert. If you tweak this code, notice that the new line character is \\n instead of \n.

function showDataForEmail(){

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var values = sheet.getDataRange().getValues();
    var msg = "";

    for(var i = 0; i < values.length; i++) {

        if(values[i][1] === "[email protected]") {

            msg += values[i][0] + " " + values[i][1] + " " + values[i][2] + " " + values[i][3] + "\\n"

        }

    }

    Browser.msgBox(msg);

}

Upvotes: 2

Related Questions