string 处理字符串的 Google 电子表格脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15071717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Spreadsheet Script working with strings
提问by barnak
I have a question about how to work with 'string' type in Google Script?
我有一个关于如何在 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.
我有一个包含不同格式站点的专栏,例如 http://testsite.com/, http:///www.sitetest.net/, dot.anothersite.com, http://anysite.net/index.html 等等。
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)
所以我想将这些记录自动格式化为一个模板:sitename.domain(例如 http://testsite.com/ = testsite.com || 网址://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
而且我不知道如何在 google 脚本中使用字符串。例如,在 C++ 中,我可以使用字符串函数,如 String.Find() 或 String.Mid()。但是我在 Google Scripts 中看不到相同的功能
回答by barnak
Well... I've found the answer on my own question :D
嗯......我在我自己的问题上找到了答案:D
Google Script uses String class from JavaScript So functions and methods from JS works also in Google Scripts..
Google Script 使用 JavaScript 中的 String 类,因此 JS 中的函数和方法也适用于 Google Scripts。
http://www.w3schools.com/jsref/jsref_obj_string.asp
http://www.w3schools.com/jsref/jsref_obj_string.asp
May be it will help somebody.
可能它会帮助某人。
回答by sumit
May be a little late..
可能有点晚了。。
but it would be good to know that many common string object methods do notwork in Google App Script. For e.g. .endsWith, .includesetc.
但它是很好的了解,许多常见的字符串对象的方法没有在谷歌应用程序脚本的工作。例如.endsWith, .includes等。
check this stackoverflow question.
检查这个 stackoverflow问题。
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")
类型错误:在对象 abc@example 中找不到包含的函数。(第 19 行,文件“main”)
as an alternative of above methods what helped me was 'indexOf' method
作为上述方法的替代方法,对我有帮助的是“indexOf”方法
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.
我希望它可以帮助某人。