Google App脚本可将值从一个单元格追加到另一个单元格中的数字字符串 [英] Google App Script to Append Value from one Cell to String of Numbers in another Cell
问题描述
我一直在尝试找出如何编写一个脚本,该脚本将从一个单元格中获取该值并将其附加到同一行另一单元格中的数字字符串的末尾.新附加的数字需要与先前附加的值之间用逗号分隔,并且整个字符串需要用方括号括起来.前任. [2,3,3,4.5,2.5,2.1,1.3,0.4].该脚本将需要从第三行开始循环遍历命名工作表上包含数据的所有行.
I’ve been trying to figure out how to write a script which will take the value from one cell and append it to the end of a string of numbers in another cell of that same row. The newly appended number needs to be separated by a comma from the previously appended value, and the whole string needs to be wrapped between brackets. EX. [2,3,3,4.5,2.5,2.1,1.3,0.4]. The script will need to loop through all of the rows containing data on a named sheet beginning with the third row.
上面的图像显然只是一个仅包含两行数据的示例.实际的电子表格将包含一千多行,因此该操作必须以编程方式完成,并且将使用定时触发器每周运行一次.
The above image is obviously just an example containing only two rows of data. The actual spreadsheet will contain well over a thousand rows, so the operation must be done programmatically and will run weekly using a timed trigger.
要尽可能具体,我需要帮助的是首先知道在Google App脚本中是否还可以进行类似附加的操作.我花了数小时进行搜索,但似乎无法找到一种方法来将新值(例如,单元格A3)附加到当前字符串(例如,单元格B3)而不完全覆盖它. 全面披露;我是一名中学老师,正试图为我的学校整理一些东西.
To be as specific as I can, what I need help with is to first know if something like the appending is even possible in Google App Scripts. I've spent hours searching and I can't seem to find a way to append a new value (ex. cell A3) to the current string (ex. cell B3) without overwriting it completely. In full disclosure; I'm a middle school teacher trying to put something together for my school.
推荐答案
要尽可能具体,我需要帮助的是首先知道在Google App脚本中是否还可以进行类似附加的操作.
To be as specific as I can, what I need help with is to first know if something like the appending is even possible in Google App Scripts.
看到预期的结果,它是插入而不是附加,因为应该在最后一个字符(]
)之前添加字符串.无论如何,是的,可以通过使用JavaScript字符串处理方法来实现.
Seeing the expected result, it's inserting rather than appending, as the string should be added before the last character (]
). Anyway, yes, this is possible by using JavaScript string handling methods.
- 使用
getValue()
获取当前GPA和GPA历史记录的单元格值. - 一种方法是使用替换
- Use
getValue()
to the get the cell values, both the Current GPA and the GPA History. - One way is to use replace
使用纯JavaScript的示例:
Example using pure JavaScript:
var currentGPA = 3.5
var gpaHistory = '[2,3.1,2.4]';
gpaHistory = gpaHistory.replace(']',','+currentGPA+']');
console.info(gpaHistory)
获得修改后的gpaHistory
后,请使用setValue(gpaHistory)
将此值添加到电子表格中.
Once you get the modified gpaHistory
, use setValue(gpaHistory)
to add this value to the spreadsheet.
这篇关于Google App脚本可将值从一个单元格追加到另一个单元格中的数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!