Google表格重复行脚本不起作用

时间:2016-10-27 17:57:10

标签: google-apps-script google-sheets

目前我有一个脚本重复行,但只保留行数据的第一个单元格。但是目前在我的电子表格上运行它时,它会为每个当前行创建15行 - 它应该为每个当前行创建4行:(

function duplicateRows() {

var sh, v, arr, c, b;

sh = SpreadsheetApp.getActive()
    .getSheetByName('products_export')
v = sh.getDataRange()
    .getValues();
arr = [v[0]];
v.splice(1)
    .forEach(function (r, i) {
        arr.push(r)
        c = 0
        while (c < 3) {
            arr.push([r[0],"","","","","","","","","","","","","","","","","","","","","",""])
            c += 1;
        }
    })
sh.getRange(1, 1, arr.length, arr[0].length)
    .setValues(arr);
}

1 个答案:

答案 0 :(得分:0)

你可能只是运行了两次这个功能。

<强>初始

N  NAME  AGE
1  fish  cat
2  fire  hat

首次执行:

N  NAME  AGE
1  fish  cat
1
1
1
2  fish  cat
2
2
2

*每个四个:

1 * 4 = 4

4 - 1(基行)= 3份

第二次执行:

N  NAME  AGE
1  fish  cat
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2  fish  cat
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

*每个

四份

4 * 4 = 16

16 - 1(基行)= 15份