【问题标题】:Trying to split string in google sheets that have more than one space尝试在具有多个空格的谷歌表格中拆分字符串
【发布时间】:2016-06-18 07:07:09
【问题描述】:
我正在尝试根据文本是否包含多个空格来拆分 A 列中的文本。当我在脚本编辑器中运行它时,没有任何运行。我不确定我需要修复什么。
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var re = new RegExp("/ +/")
function rename() {
for(n=0;n<values.length;++n){
var cell = values[n][0] ;
var result = cell.split(re);
}
}
rename();
【问题讨论】:
标签:
google-apps-script
google-sheets
【解决方案1】:
请参阅 Troubleshooting,了解如何在 Google Apps Scripts 上对您的脚本进行故障排除。
关于您的代码,我在下面添加了它,并用内联 cmets 描述了一些细微的变化。
注意:在数据范围较小的工作表上进行测试。
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var re = new RegExp(" +"); // Slashes removed
function rename() {
for(n=0;n<values.length;n++){ // plus sign moved after n
var cell = values[n][0] ;
var result = cell.split(re);
Logger.log(result); // This line will write the result text to the debugging logs.
}
}
// rename(); this line should not be included.