我有一个谷歌文档电子表格,其中包含如下行:
| | things; separated;by;semicolons;with;line;breaks |
something | something else | A;B;C;D;E;F;G | one more thing
| | K;L;M;N;O;P;Q |
-----------------------------------------------------------------------------------------------
我想用换行符分割第三列,将每一列放在整行的一个副本中,覆盖第三个单元格,然后用分号分割那个单元格。最后,前一行应转换为如下内容:
something|something else|things|separated|by|semicolons|with|line|breaks| one more thing
-----------------------------------------------------------------------------------------
something|something else| A | B | C| D | E | F | G | one more thing
-----------------------------------------------------------------------------------------
something|something else| K | L | M| N | O | P | Q | one more thing
-----------------------------------------------------------------------------------------
我尝试了这个功能,但有些单元格重叠并覆盖了其他单元格:
function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
var output = [];
for (i in anArray){ // for each row
var splitArray = anArray[i][splitColumnIndex].split(/\n/g); // split values by line breaks
for (j in splitArray){ // for each split array
var row = anArray[i].slice(0); // copies the entire line
row.splice(2,1,splitArray[j].split(";")); // inserts the splited values overwriting the third cell
output.push(row);
}
}
return output;
}
所以 splice 函数不会在单元格 2 和 4 之间插入拆分值。拆分值从第三个单元格开始放置,覆盖第四个单元格(在示例中为“另一件事”),如下所示:
something|something else|things|separated|by|semicolons|with|line|breaks
------------------------------------------------------------------------
有人能指出我的错误在哪里吗?我希望我解释得足够好。
提前致谢。