Reputation: 499
I have a question about how to work with 'string' type in Google Script?
I have a column with sites in different formats, e.g. http://testsite.com/, http://www.sitetest.net/, dot.anothersite.com, http://anysite.net/index.html and so on.
So I want to automatically format these records to one template: sitename.domain (for example http://testsite.com/ = testsite.com || http://www.sitetest.net/ = sitetest.net)
I get this info by this script:
var sheet = SpreadsheetApp.getActiveSheet();
var sheetName = sheet.getName();
var restrictSheet = "Summary";
if (sheetName != restrictSheet){ // I don't need to modify first sheet from my spreadsheet
var sheetRange = sheet.getActiveRange();
var sData = sheet.getRange("A3:A"); // column with sites
var TextArray = sData.getValues();
}else Browser.msgBox("wrong sheet");
}
And I don't know how to work with strings in google script. In C++, for example, I could use string functions like String.Find() or String.Mid(). But I can't see equal functions in Google Scripts
Upvotes: 13
Views: 89536
Reputation: 3710
May be a little late..
but it would be good to know that many common string object methods do not work in Google App Script. For e.g. .endsWith, .includes etc.
check this stackoverflow question.
here are a few tests to verify
function stringEndsWithTest(){
var a = "[email protected]";
SpreadsheetApp.getUi().alert('endsWith method on a string in google app script '+ a.endsWith('.com'));
}
function stringIncludesTest(){
var a = "[email protected]";
SpreadsheetApp.getUi().alert('includes method on a string in google app script '+ a.includes('.com'));
}
I was getting this error at the time of writing this answer
TypeError: Cannot find function includes in object abc@example. (line 19, file "main")
as an alternative of above methods what helped me was 'indexOf' method
function stringIndexOfTest(){
var a = "[email protected]";
SpreadsheetApp.getUi().alert('indexOf method on a string in google app script '+ a.indexOf('.com'));
}
I hope it might help someone.
Upvotes: 13
Reputation: 499
Well... I've found the answer on my own question :D
Google Script uses String class from JavaScript So functions and methods from JS works also in Google Scripts..
http://www.w3schools.com/jsref/jsref_obj_string.asp
May be it will help somebody.
Upvotes: 36