Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 对于每行,添加重复项,更改2个单元格_Google Apps Script - Fatal编程技术网

Google apps script 对于每行,添加重复项,更改2个单元格

Google apps script 对于每行,添加重复项,更改2个单元格,google-apps-script,Google Apps Script,我有一堆行,我想附加重复项,除了更改两个单元格。 我需要每个人为每个24岁和36岁的孩子排15000、20000、25000人的队(如果这有意义的话?) 输入: A B C D 1 15000 24 Susan Smith 2 15000 24 John Deer 预期产量 A B C D

我有一堆行,我想附加重复项,除了更改两个单元格。 我需要每个人为每个24岁和36岁的孩子排15000、20000、25000人的队(如果这有意义的话?)

输入:

        A          B        C         D
1     15000       24      Susan     Smith
2     15000       24      John      Deer
预期产量

        A          B        C         D
1     15000       24      Susan     Smith
2     20000       24      Susan     Smith
3     25000       24      Susan     Smith
4     15000       36      Susan     Smith
5     20000       36      Susan     Smith
6     25000       36      Susan     Smith
7     15000       24      John      Deer
8     20000       24      John      Deer
9     25000       24      John      Deer
10    15000       36      John      Deer
11    20000       36      John      Deer
12    25000       36      John      Deer

我知道我需要为每一行执行一个复制和追加该行的函数,但我不确定这是如何实现的。

我相信您的目标如下

  • 您希望在问题中实现转换

    • 例如,当列“C”和“D”的值分别为
      Susan
      Smith
      时,您希望将以下值放入电子表格

        15000       24      Susan     Smith
        20000       24      Susan     Smith
        25000       24      Susan     Smith
        15000       36      Susan     Smith
        20000       36      Susan     Smith
        25000       36      Susan     Smith
      
  • 您希望使用谷歌应用程序脚本实现这一点

在这种情况下,我想提出以下流程

  • 从源工作表的“C”和“D”列中检索值
  • 删除空行
  • 创建一个数组,分别使用列“A”和列“B”的
    15000、20000、25000和
    24、36
    的值来输入值
  • 将值放入目标工作表
  • 当上述流程反映到GoogleApps脚本时,它将变成如下所示

    示例脚本: 请将以下脚本复制并粘贴到电子表格的脚本编辑器中。并且,请设置
    srcSheetName
    dstSheetName
    的变量,并运行
    myFunction
    的函数

    function myFunction() {
      const srcSheetName = "Sheet1";  // Please set the source sheet name.
      const dstSheetName = "Sheet2";  // Please set the destination sheet name.
    
      const ss = SpreadsheetApp.getActiveSpreadsheet();
    
      // 1. Retrieve the values from the columns "C" and "D".
      const srcSheet = ss.getSheetByName(srcSheetName);
      const values = srcSheet.getRange("C1:D" + srcSheet.getLastRow()).getValues();
    
      // 2. Remove the empty rows.
      const v = values.filter(([c,d]) => c && d);
    
      // 3. Create an array for putting values using the values of `15000, 20000, 25000` and `24, 36` for the columns "A" and "B", respectively.
      const colA = [15000, 20000, 25000];
      const colB = [24, 36];
      const res = v.reduce((ar, [c,d]) => {
        colB.forEach(b => colA.forEach(a => ar.push([a, b, c, d])));
        return ar;
      }, []);
    
      // 4. Put the values to the destination sheet.
      const dstSheet = ss.getSheetByName(dstSheetName);
      dstSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
    }
    
    注:
    • 如果要将上述脚本用作自定义函数,还可以使用以下脚本。在这种情况下,请将以下脚本复制并粘贴到电子表格的脚本编辑器中。并且,请将自定义函数
      =SAMPLE(C1:D)
      放入单元格。由此,获得结果值

        const SAMPLE = values => values
          .filter(([c,d]) => c && d)
          .reduce((ar, [c,d]) => {
            [24, 36].forEach(b => [15000, 20000, 25000].forEach(a => ar.push([a, b, c, d])));
            return ar;
          }, []);
      
    参考资料: