gpt4 book ai didi

javascript - 如何将拆分的 Google 电子表格单元格值推送到相邻单元格

转载 作者:行者123 更新时间:2023-11-28 00:57:02 25 4
gpt4 key购买 nike

我正在使用 Martin Hawksey 的 Google Apps 脚本的修改版本,该脚本将 Google 电子表格中的一张工作表中的数据放入另一张工作表中,为我的事件描述列中的每个条目创建一个新行(用逗号分隔 - 即cell D2="描述 1、描述 2、描述 3 等)。虽然这非常有效,但我希望它再执行一项功能 - 我希望它获取事件描述中的最后一个条目(也许用分号而不是逗号将其与其他单元格分隔开),并为每个唯一事件将其插入相邻单元格一次且仅一次,同时删除分号或其他分隔符。我尝试了多种方法,但对于 Google Apps 来说还是个新手脚本,我没有成功。

function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
var output = [];
for (i in anArray){ // for each row
var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column
for (j in splitArray){ // for each split cell value
if(splitArray[j]=="" && j>=1)
continue;
var row = anArray[i].slice(0); // take a copy of source row
row[splitColumnIndex] = alltrim(splitArray[j]); // replace comma separated value with current split value
output.push(row); // push new row to output
}
}
return output;
}

function alltrim(str) {
return str.replace(/^\s+|\s+$/g, '');
}

本质上,这就是我想要做的 - 转动它:

Date     Client     County     Description
9/21/14 12345 Greene Location 1, Location 2, Location 3; Y
9/24/14 54321 Brown Location 1, Location 2; X

进入此:

Date     Client     County     Description     Time
9/21/14 12345 Greene Location 1
9/21/14 12345 Greene Location 2
9/21/14 12345 Greene Location 3 Y
9/24/14 54321 Brown Location 1
9/24/14 54321 Brown Location 2 X

任何帮助将不胜感激!

谢谢!

最佳答案

这个编辑后的 ​​splitColumnAndRepeatRows() 函数将执行您想要的操作,还有一个额外的好处,即列中任何(不仅仅是最后一个)逗号分隔的项目都可以是一个 ; 分隔的列表,并将被分成 2 个单元格.

function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
var output = [];
for (var i in anArray){ // for each row
var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column
for (var j in splitArray){ // for each split cell value
if(splitArray[j]=="" && j>=1)
continue;
var row = anArray[i].slice(0); // take a copy of source row
var trimmedString = alltrim(splitArray[j]);
var subArray = trimmedString.split(";"); // split current item of specified column at ;
if ( subArray.length > 1 ) { // if current item is a ;-delimited list (now split into an array)
row[splitColumnIndex] = alltrim(subArray[0]); // replace comma-separated value with first item of ;-delimited array
row[splitColumnIndex+1] = alltrim(subArray[1]); // append second item of ;-delimited list as new cell to row
}
else {
row[splitColumnIndex] = trimmedString; // replace comma separated value with current split value
row[splitColumnIndex+1] = ""; // append empty cell to row
}
output.push(row); // push new row to output
}
}
return output;
}

它可以变成这样:

9/21/14  12345      Greene     Location 1; A, Location 2, Location 3; B
9/24/14 54321 Brown Location 1, Location 2; C

进入此:

9/21/14  12345      Greene     Location 1      A
9/21/14 12345 Greene Location 2
9/21/14 12345 Greene Location 3 B
9/24/14 54321 Brown Location 1
9/24/14 54321 Brown Location 2 C

关于javascript - 如何将拆分的 Google 电子表格单元格值推送到相邻单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com