我想用'|'从数组中拆分值分隔器。 值拆分将被写入,再增加一列
UID NAME MTD DEVICE APPLICATION APPLICATION REV
ABC Melanie 10230 1520 google |mozilla 1.2|4
COCO Ronald 222222 2682 explorer|ecosia 2.0|3
ZERTY Cecilia 55555 634 google|ecosia 1.001|2.02
收件人:
UID NAME MTD USAGE DEVICE APPLICATION APPLICATION REV
ABC Melanie 10230 1520 google mozilla 1.2 4
COCO Ronald 222222 2682 explorer ecosia 2 3
ZERTY Cecilia 55555 634 google ecosia 1.001 2.02
我尝试了函数拆分,但是无法替换以获取整个范围值。在这里,我只能拆分一个单元格。
function Separate() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet29');
var lr = spreadsheet.getLastRow();
var lc = spreadsheet.getLastColumn();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet29');
var range1 = sheet1.getRange(1, 1, sheet1.getLastRow(), 13);
var values1 = range1.getValues();;
var cellToSplit = [];
cellToSplit = spreadsheet.getRange(2, 5, sheet1.getLastRow()).getValue();
Logger.log(cellToSplit);
var cellArray = cellToSplit.split("|"); // Splits the cell value by ',' and stores it in array.
Logger.log(cellArray); //Array of 2 values
for (var i = 0; i < cellArray.length; i++) {
Logger.log(cellArray[i]); //Separate only one cell
}
答案 0 :(得分:0)
分割所有值而不是仅分割“ |”会更容易。首先,使用您所说的split()函数,但是像这样:
var values = spreadsheet.getRange(2, 5, sheet1.getLastRow()).getValue();
var cellTosplit = values.split(/[\s| ]+/); //here we separate by spaces and by |
当您想将它们再次写到工作表中时,一种选择是将单元格存储在2D数组中:
var array2d = [[],[]];
var pos = 0; //position of the cells array
for (var y = 0; y < rows; y++) {
array2d[y] = [];
for (var x = 0; x <= columns; x++) {
array2d[y][x] = cellsTosplit[pos]; //Or you can use this loop to directly write the values in the range you want instead, instead of using the 2D array
pos++;
}
}
现在,您可以在所需的单元格中写入array2d[row][column]
。如我所说,您可以创建另一个foor循环以遍历Range并使用setValues(array2d[y][x])
,或使用相同的循环而不是2D数组。