我试图在一个选项卡中填充单元格范围,并使用从我的Google表单中的其他选项卡收集的特定值。但是,我无法为它编写正确的脚本。在一个选项卡中填充单元格,并从Google表格中的其他选项卡中收集特定值
这里有一个更详细的解释:
我在谷歌文档床单几个标签,代表了一年中的一个月,即他们命名为July 2017
,December 2016
等。另外我还有两个名为Graphs
和Blank
的选项卡。
其N6
单元格中的每个月标签都具有这样的值:Balance 33050,32 UAH
(该数字可能因标签而异)。
我想要做的事:
我需要收集来自我所有的一个月标签的N6
值,并填写以下值到Graphs
标签的A
列。然后,我需要从我收集的所有值中构建一个图表。但现在的主要问题是正确收集值。
我试图为此编写一个脚本,但似乎我做了一些不正确的事情。我对google脚本很陌生,所以也许我在这里错过了一些简单的东西。
这里是我的脚本:
var months = [
"january","february","march","april","may","june","july","august","september","october", "november","december"
];
var graphSheetName = 'Graphs';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var graphSheet = ss.getSheetByName(graphSheetName);
// this function tells if the name of the tab refers to one of the month tabs.
function isMonthSheet(name) {
return matchInArray(name, months);
}
function matchInArray(string, expressions) {
var len = expressions.length,
i = 0;
for (; i < len; i++) {
if (string.toLowerCase().match(expressions[i])) {
return true;
}
}
return false;
}
// This function runs on every edit of the google sheets document.
// It should fill the cells in the `Graphs` tab.
function getMonthsTotals() {
var sheets = ss.getSheets(); // get all tabs
var values = [];
for (var i = 0; i < sheets.length; ++i) {
var sheet = sheets[i];
// if it is a month tab, collect the N6 value into the `values` array.
// "Balance 33050,32 UAH".slice(8,-4) --> "33050,32".
if (isMonthSheet(sheet.getName())) {
var value = sheet.getRange('N6').getValue().slice(8, -4);
values.push(value);
}
}
// fill the `Graphs` tab with the collected values into the `A` column.
var rangeStr = 'A1:A' + values.length;
graphSheet.getRange(rangeStr).setValues(values);
}
正如我已经上面说的,我是很新的Google表格的脚本,所以我不是很熟悉它的调试,以及,所以我的方法是使用toast
函数。因此,我不能提供有关脚本如何实际运行的很多信息。
我寻求建议和帮助如何纠正我的脚本,使其正确运行。
“看来,我做了什么错误。”无助于诊断问题。结果与预期有什么不同? – FTP