复制具有条件的行(google 应用程序脚本)
Duplicate a row with a condition (google app script)
我有一个 sheet 其中一栏(第 10 栏)是一个下拉列表,有 2 种可能性:官方或非官方。我正在尝试创建一个脚本,将此 sheet 导出到另一个具有这种条件的脚本:第 10 列中带有“官方”的每一行都必须复制为“非官方”(因此在这种情况下我们将有 2一排官方和一排非官方)
所以这是我的代码:
const tab = active_sheet.getRange("A2:M14").getValues();
for (let nbline=0; nbline <tab.length;nbline++) {
if (tab[nbline][10] == "official") {
而且我没有找到复制此行的命令,保留所有信息并将第 10 列更改为“非官方”
例如这是“制表符”:
第 1 行:a b c d e 官方 f g h
第 2 行:1 2 3 4 5 非官方 6 7 8
第 3 行:x c v b x 官方 m l k
第 4 行:n j i o k 非官方 6 9 8
我想要这个:
第 1 行:a b c d e 官方 f g h
第 2 行:a b c d e 非官方 f g h
第 3 行:1 2 3 4 5 非官方 6 7 8
第 4 行:x c v b x 官方 m l k
第 5 行:x c v b x 非官方 m l k
第 6 行:n j i o k 非官方 6 9 8
如果有人能帮助我
提前致谢!
脚本:
function myFunction() {
const active_sheet = SpreadsheetApp.getActiveSheet();
const tab = active_sheet.getRange("A2:M14").getValues();
var output = [];
// if we are checking the 10th column, column should be 9 instead (0-index),
// modify if needed.
var column = 10;
tab.forEach(row => {
output.push(row);
if(row[column] == "official")
// directly modifying row[10] and pushing row will result
// to the column having "non official" to all rows
output.push([...row.slice(0, column), "non official", ...row.slice(column + 1)]);
})
active_sheet.getRange(2, 1, output.length, output[0].length).setValues(output)
}
样本:
输出:
注:
- 您提到了第 10 列,但在第 11 列的代码中使用了
[10]
。我直接修改了你的脚本,所以我使用了 10
,但如果你真的想要第 10 列,请随意将列修改为 9
。
- 直接将
row[column]
更改为 "non official"
并推送相同的 row
或等同于 row
的变量时出现一些意外问题。它正确地复制了行,但列值都是 "non official"
.
- 如果有第14行之后的行,有重复的行会被覆盖。包括
tab
范围内的所有行以避免它发生。
我有一个 sheet 其中一栏(第 10 栏)是一个下拉列表,有 2 种可能性:官方或非官方。我正在尝试创建一个脚本,将此 sheet 导出到另一个具有这种条件的脚本:第 10 列中带有“官方”的每一行都必须复制为“非官方”(因此在这种情况下我们将有 2一排官方和一排非官方)
所以这是我的代码:
const tab = active_sheet.getRange("A2:M14").getValues();
for (let nbline=0; nbline <tab.length;nbline++) {
if (tab[nbline][10] == "official") {
而且我没有找到复制此行的命令,保留所有信息并将第 10 列更改为“非官方”
例如这是“制表符”:
第 1 行:a b c d e 官方 f g h
第 2 行:1 2 3 4 5 非官方 6 7 8
第 3 行:x c v b x 官方 m l k
第 4 行:n j i o k 非官方 6 9 8
我想要这个:
第 1 行:a b c d e 官方 f g h
第 2 行:a b c d e 非官方 f g h
第 3 行:1 2 3 4 5 非官方 6 7 8
第 4 行:x c v b x 官方 m l k
第 5 行:x c v b x 非官方 m l k
第 6 行:n j i o k 非官方 6 9 8
如果有人能帮助我
提前致谢!
脚本:
function myFunction() {
const active_sheet = SpreadsheetApp.getActiveSheet();
const tab = active_sheet.getRange("A2:M14").getValues();
var output = [];
// if we are checking the 10th column, column should be 9 instead (0-index),
// modify if needed.
var column = 10;
tab.forEach(row => {
output.push(row);
if(row[column] == "official")
// directly modifying row[10] and pushing row will result
// to the column having "non official" to all rows
output.push([...row.slice(0, column), "non official", ...row.slice(column + 1)]);
})
active_sheet.getRange(2, 1, output.length, output[0].length).setValues(output)
}
样本:
输出:
注:
- 您提到了第 10 列,但在第 11 列的代码中使用了
[10]
。我直接修改了你的脚本,所以我使用了10
,但如果你真的想要第 10 列,请随意将列修改为9
。 - 直接将
row[column]
更改为"non official"
并推送相同的row
或等同于row
的变量时出现一些意外问题。它正确地复制了行,但列值都是"non official"
. - 如果有第14行之后的行,有重复的行会被覆盖。包括
tab
范围内的所有行以避免它发生。