49

I have a google form that when the user submits it will trigger my function to run which is creating a summary of what they submitted as a Google Doc. I know it can automatically send an email but I need it formatted in a way that my user can edit it later.

There are some check boxes on the form -- but the getResponse() is only populated with the items checked and I need it to show all possible choices. Then I will indicate somehow what was checked.

I can't find a way to see if a text contains a value. Like in Java with a String, I could do either .contains("9th") or .indexOf("9th") >=0 and then I would know that the String contains 9th. How can I do this with google scripts? Looked all through documentation and I feel like it must be the easiest thing ever.

var grade = itemResponse.getResponse();

Need to see if grade contains 9th.

Marios
  • 26,333
  • 8
  • 32
  • 52
Sharon
  • 493
  • 1
  • 4
  • 4
  • Just in case you didn't know, you can use [markdown](http://Stack Overflow/editing-help) to format code snippets in the body of your post. This helps make your question more concise and readable. – brandonscript Dec 11 '13 at 19:20

4 Answers4

73

Google Apps Script is javascript, you can use all the string methods...

var grade = itemResponse.getResponse();
if(grade.indexOf("9th")>-1){do something }

You can find doc on many sites, this one for example.

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thank you! I swear I tried that, along with 20 other things, but now it works! – Sharon Dec 12 '13 at 12:32
  • glad it helped (I'm sure you did but some detail must have been wrong I guess, things like that happen :-) - please consider accepting the answer. – Serge insas Dec 12 '13 at 13:48
  • 15
    Note: endsWith() and starsWith() are not Javascript standard until ECMAScript 6 (2015), so Google Apps Script does not support these 2, even though they are in the w3schools.com link you gave, when most browsers support them. To support these 2 functions, you can add String.prototype.startsWith = function(prefix) { return this.indexOf(prefix) === 0; } String.prototype.endsWith = function(suffix) { return this.match(suffix+"$") == suffix; }; as in http://rickyrosario.com/blog/javascript-startswith-and-endswith-implementation-for-strings/ – Ben Lin Feb 19 '16 at 18:56
21

Update 2020:

You can now use Modern ECMAScript syntax thanks to V8 Runtime.

You can use includes():

var grade = itemResponse.getResponse();
if(grade.includes("9th")){do something}
Marios
  • 26,333
  • 8
  • 32
  • 52
7

I had to add a .toString to the item in the values array. Without it, it would only match if the entire cell body matched the searchTerm.

function foo() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var s = ss.getSheetByName('spreadsheet-name');
    var r = s.getRange('A:A');
    var v = r.getValues();
    var searchTerm = 'needle';
    for(var i=v.length-1;i>=0;i--) {
        if(v[0,i].toString().indexOf(searchTerm) > -1) {
            // do something
        }
    }
};
Marios
  • 26,333
  • 8
  • 32
  • 52
GraehamF
  • 1,971
  • 24
  • 24
2

I used the Google Apps Script method indexOf() and its results were wrong. So I wrote the small function Myindexof(), instead of indexOf:

function Myindexof(s,text)
{
  var lengths = s.length;
  var lengtht = text.length;
  for (var i = 0;i < lengths - lengtht + 1;i++)
  {
    if (s.substring(i,lengtht + i) == text)
      return i;
  }
  return -1;
}

var s = 'Hello!';
var text = 'llo';
if (Myindexof(s,text) > -1)
   Logger.log('yes');
else
   Logger.log('no');
Marios
  • 26,333
  • 8
  • 32
  • 52
Oscar
  • 31
  • 1
  • I don't think you need a routine. .includes will return true or false and result of indexOf can be checked for greater than zero. var src = 'Hello!'; var text = 'llo'; var idx = src.indexOf(text); Logger.log("indexOf(text) idx: " + idx ); // result: 2 if (src.indexOf(text) > 0 ) Logger.log('Yes'); else Logger.log('No'); // result: Yes Logger.log("src.includes(text): " + src.includes(text) ); // result: true if (Myindexof(src,text) > -1) Logger.log('yes'); else Logger.log('no'); // result: yes } – aNewb Nov 22 '20 at 23:06