假如说:
A1 = 3
B1 = customFunc(A1) // will be 3
在我的自定义函数中:
function customFunc(v) {
return v;
}
v
将是 3。但我想访问单元格对象 A1
.以下内容是从下面的评论中转录的。
输入:
+---+---+
| | A |
+---+---+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+---+---+
我要复制
A1:A4
至 B1:C2
使用自定义函数。想要的结果:
+---+---+---+---+
| | A | B | C |
+---+---+---+---+
| 1 | 1 | 1 | 2 |
| 2 | 2 | 3 | 4 |
| 3 | 3 | | |
| 4 | 4 | | |
+---+---+---+---+
最佳答案
要实现将输入列表拆分为多行的预期结果,您可以尝试以下方法。
function customFunc(value) {
if (!Array.isArray(value)) {
return value;
}
// Filter input that is more than a single column or single row.
if (value.length > 1 && value[0].length > 1) {
throw "Must provide a single value, column or row as input";
}
var result;
if (value.length == 1) {
// Extract single row from 2D array.
result = value[0];
} else {
// Extract single column from 2D array.
result = value.map(function (x) {
return x[0];
});
}
// Return the extracted list split in half between two rows.
return [
result.slice(0, Math.round(result.length/2)),
result.slice(Math.round(result.length/2))
];
}
请注意,它不需要使用单元格引用。它纯粹是处理输入的二维数组并返回转换后的二维数组。
使用该函数会产生以下结果:
A1:A4
是硬编码的,B1
包含 =customFunc(A1:A4)
+---+---+---+---+
| | A | B | C |
+---+---+---+---+
| 1 | a | a | b |
| 2 | b | c | d |
| 3 | c | | |
| 4 | d | | |
+---+---+---+---+
A1:D4
是硬编码的,A2
包含 =customFunc(A1:D4)
+---+---+---+---+---+
| | A | B | C | D |
+---+---+---+---+---+
| 1 | a | b | c | d |
| 2 | a | b | | |
| 3 | c | d | | |
+---+---+---+---+---+
A1:B2
是硬编码的,A3
包含 =customFunc(A1:B2)
,错误消息是“必须提供单个值、列或行作为输入”+---+---+---+---------+
| | A | B | C |
+---+---+---+---------+
| 1 | a | c | #ERROR! |
| 2 | b | d | |
+---+---+---+---------+
通过处理更多参数(即要拆分的行数、每行的项目数、拆分为行而不是列等)或分析值本身,可以构建此方法以执行更复杂的转换。
通过创建一个将函数作为参数的函数来执行任意转换的快速示例。
但是,这种方法有以下限制:
功能:
/**
* @param {Object|Object[][]} value The cell value(s).
* @param {function=} opt_transform An optional function to used to transform the values.
* @returns {Object|Object[][]} The transformed values.
*/
function customFunc(value, opt_transform) {
transform = opt_transform || function(x) { return x; };
if (!Array.isArray(value)) {
return transform(value);
}
// Filter input that is more than a single column or single row.
if (value.length > 1 && value[0].length > 1) {
throw "Must provide a single value, column or row as input";
}
var result;
if (value.length == 1) {
// Extract single row from 2D array.
result = value[0].map(transform);
} else {
// Extract single column from 2D array.
result = value.map(function (x) {
return transform(x[0]);
});
}
// Return the extracted list split in half between two rows.
return [
result.slice(0, Math.round(result.length/2)),
result.slice(Math.round(result.length/2))
];
}
并进行快速测试:
function test_customFunc() {
// Single cell.
Logger.log(customFunc(2, function(x) { return x * 2; }));
// Row of values.
Logger.log(customFunc([[1, 2, 3 ,4]], function(x) { return x * 2; }));
// Column of values.
Logger.log(customFunc([[1], [2], [3], [4]], function(x) { return x * 2; }));
}
其中记录以下输出:
[18-06-25 10:46:50:160 PDT] 4.0
[18-06-25 10:46:50:161 PDT] [[2.0, 4.0], [6.0, 8.0]]
[18-06-25 10:46:50:161 PDT] [[2.0, 4.0], [6.0, 8.0]]
关于google-apps-script - 如何将单元格引用传递给 Apps 脚本自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50997634/