我从列中获得以下示例:
ABC1 Walsall - ABC: Ground Floor, One Tower, 123 Bridge Street, London AB1 1JZ
ADF6 Wembley
KLM1 Bedford - ABC: 6 Another Street, Birmingham CD40 1HP
有些单元格为空白或格式不同,但都以4个字符的代码开头,然后是城市。
我想减去该代码并将其放入一个新的数组和城市名称中。
我尝试了以下脚本,但它不起作用。
function myFunction() {
var ss = SpreadsheetApp.openById("ID");
var sheet = ss.getSheetByName("Subtract");
var range = sheet.getDataRange();
var data = range.getValues();
var locCode = [];
for (i=1; i<data.length; i++) {
locCode.push(data[i][0]);
}
locCode.slice(5, locCode.length);
Logger.log(locCode);
}
你可以帮帮我吗?
谢谢!
答案 0 :(得分:0)
假设您的代码总是有4个字符,后跟城市名称,可选地后跟空格,短划线和其他字符
var cities = [];
for (i = 1; i < data.length; i++) {
var cellText = data[i][0];
// extract the 4 first characters
locCode.push(cellText.substring(0, 4));
// add " -" at the end of text to make sure even 'ADF6 Wembley' has the
// 'CODE CITY - ...' format,
// then extract any thing after the code and before the first "-", ignoring the space
cities.push(cellText.substring(5, (cellText + " -").indexOf("-") - 1))
}