bar*_*nak 13 string google-sheets google-apps-script
我有一个关于如何在Google Script中使用'string'类型的问题?
我有一个列有不同格式的网站,例如http://testsite.com/,http://www.sitetest.net/,dot.anothersite.com,http://anysite.net/index.html等.
所以我想自动将这些记录格式化为一个模板:sitename.domain(例如http://testsite.com/ = testsite.com || HTTP://www.sitetest.net/ = sitetest.net)
我通过这个脚本得到这个信息:
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");
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用谷歌脚本中的字符串.例如,在C++中,我可以使用字符串函数,如String.Find()或String.Mid().但我在Google Scripts中看不到相同的功能
bar*_*nak 35
嗯......我在自己的问题上找到了答案:D
Google Script使用JavaScript中的String类所以JS中的函数和方法也适用于Google Scripts.
http://www.w3schools.com/jsref/jsref_obj_string.asp
可能它会帮助某人.
可能有点晚了..
但它是很好的了解,许多常见的字符串对象的方法没有在谷歌应用程序脚本工作.例如.endsWith,.includes等.
检查这个stackoverflow 问题.
这里有一些要验证的测试
function stringEndsWithTest(){
var a = "abc@example.com";
SpreadsheetApp.getUi().alert('endsWith method on a string in google app script '+ a.endsWith('.com'));
}
function stringIncludesTest(){
var a = "abc@example.com";
SpreadsheetApp.getUi().alert('includes method on a string in google app script '+ a.includes('.com'));
}
Run Code Online (Sandbox Code Playgroud)
在写这个答案的时候我收到了这个错误
TypeError:找不到包含在对象abc @ example中的函数.(第19行,文件"main")
作为上述方法的替代方案,帮助我的是'indexOf'方法
function stringIndexOfTest(){
var a = "abc@example.com";
SpreadsheetApp.getUi().alert('indexOf method on a string in google app script '+ a.indexOf('.com'));
}
Run Code Online (Sandbox Code Playgroud)
我希望它可以帮助某人.