gpt4 book ai didi

javascript - 工作表自定义函数 - 数组?或范围?有什么不同?

转载 作者:行者123 更新时间:2023-11-28 07:26:41 25 4
gpt4 key购买 nike

我正在尝试编写最基本的函数并苦苦挣扎该脚本的目的是获取 2 个输入(范围),然后使用这些单元格构建一个由所有单元格连接的内容组成的单个长字符串。 ..它不起作用(参见下面的代码)。

这与范围实际上不是数组有关,因为当我对数组进行硬编码时,它工作得很好。如果我有这个布局:

Header1         Header2Comment1        Comment2Comment1a       Comment2a

Note that each string is in its own cell in the spreadsheet. What I want is to get:

Header1    Comment1    Comment1aHeader2    Comment2    Comment2a

BUT all of this should be in a single cell.

Instead, what happens is:

Header1, Header2Comment1, Comment1a, Comment2, Comment2a

all in a single cell.

Here is my current code....totally confused and no documentation anywhere intelligible doesn't help.

function concatComments(headerRange,commentRange)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var out=''
newline = String.fromCharCode(10)

hVals = headerRange.split()
cVals = commentRange.split()
return hVals

// Test whether input is an array.
if (headerRange.map && commentRange.map)
{
/* process array or range */
// headerRange = ['Header1','Header2']
// commentRange = ['comment1','comment2', 'comment2a']

// var hValues = ss.getRange(headerRange).getValues()
// var cValues = ss.getRange(commentRange).getValues()

for( i=0; i< commentRange.length; i++)
{
if(commentRange[i].length >= 0)
{
out += headerRange[i] + newline + " " + commentRange[i] + newline
}
}
}
else
{
/*process single value */
out = headerRange[0] + newline + " " + commentRange[0]
}
return out
}

如果有人能解释发生了什么,我将不胜感激。

提前致谢。

保罗

最佳答案

您也可以在不使用 Apps 脚本的情况下使用 JOIN Sheets 功能来执行此操作,例如

=JOIN(" ", A1:A3)

将连接 A1:A3 中的值,并用空格分隔。

对于您的示例,您需要更复杂的东西,例如

=JOIN(CHAR(10),A1, ARRAYFORMULA("   " & A2:A3), B1, ARRAYFORMULA("   " & B2:B3))

[Example spreadsheet ]

关于javascript - 工作表自定义函数 - 数组?或范围?有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573752/

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