【问题标题】:Copy Specific Data in one Google Spreadsheet to another Google Spreadsheet with Google Apps Script使用 Google Apps 脚本将一个 Google 电子表格中的特定数据复制到另一个 Google 电子表格
【发布时间】:2019-01-13 16:33:59
【问题描述】:

我有一个电子表格供人们填写,然后点击提交按钮。该按钮会创建一个 PDF 并将该数据通过电子邮件发送给人们。每次有人提交数据时,都需要在另一个电子表格中编译部分数据。

我的代码可以复制范围并将其粘贴到电子表格中。

function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('abcd1234'); // sss = source spreadsheet
  var ss = sss.getSheetByName('Timesheet'); // ss = source sheet
  //Get full range of data
  var SRange = ss.getRange("A10:L22");
  //get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();
  //get the data values in range
  var SData = SRange.getValues();

  var tss = SpreadsheetApp.openById('abcd1234'); // tss = target spreadsheet
  var ts = tss.getSheetByName('Info'); // ts = target sheet
  var TRange = ts.getRange("A1:L13");
  var T1Range = TRange.getA1Notation();

  //set the target range to the values of the source data
  ts.getRange(T1Range).setValues(SData);

}

我对这段代码有两个问题

  1. 我无法选择特定的数据单元格,我需要选择 J6 以及整个第 11 行。如果它在 A14 中看到一个值,那么它会复制整个第 14 行。但如果 A14 中没有任何内容,则不会复制该数据。
  2. 每次提交数据时数据都会自行覆盖,我需要每次添加新数据都显示在最后一个下方。

几个小时以来,我一直在寻找解决方案,但对 Javascript 的精通不足以编写任何自定义内容。谢谢!

【问题讨论】:

    标签: javascript excel google-apps-script google-sheets spreadsheet


    【解决方案1】:

    在下面的脚本中,您可以看到如何选择单个单元格或整行,以及如何将它们的内容复制到另一个工作表中。

    还有一个示例说明如何检查单元格是否有值。

    您可以找到解决方案 here 如何将内容复制到现有工作表中而不会覆盖其中已有的内容。

    function CopyDataToNewFile() {
        
        var sourceSheet = SpreadsheetApp.openById('ID'); // Selects your Source Spreadsheet by its ID
        var ssheet = sourceSheet.getSheetByName('Timesheet'); // Selects your Source Sheet by its Name
       
        var J6value = ssheet.getRange(6, 10).getValue();  // Get value from J6 in Source Sheet
        var A14value = ssheet.getRange(14, 1).getValue(); // get value from A14 in Source Sheet
        
        var the11thRow = ssheet.getRange('A11:11').getValues(); // Select the entire 11th row in the Source Sheet
        var the14thRow = ssheet.getRange('A14:14').getValues(); // Select the entire 14th row in the Source Sheet
           
        var targetSheet = SpreadsheetApp.openById('ID'); // Selects your Target Spreadsheet by its ID
        var tsheet = targetSheet.getSheetByName('Info'); // Selects your Target Sheet by its name
        
        var targetFor14thRow = tsheet.getRange('A11:11'); // Selects where your 11th row will be copied to, in this case simply row 11
        var targetFor11thRow = tsheet.getRange('A14:14'); // Selects where your 14th row will be copied to, in this case simply row 14
          
        if (!ssheet.getRange(14, 1).isBlank()) { // Conditional: If A14 in Source Sheet isn't empty
          targetFor14thRow.setValues(the14thRow); // Then copy the entire 14th row to its target
        }
              
        tsheet.getRange(1,2).setValue(J6value); // Copy the value of J6 in the source document to 2A of the Target Sheet
        tsheet.getRange(3,4).setValue(A14value); // Copy the value of A12 in the source document to C4 of the Target Sheet    
      }

    编辑:使用 .setValues 而不是 .copyTo 来复制整行。

    【讨论】:

    • 感谢所有有用的信息,我收到错误“目标范围和源范围必须在同一个电子表格上。”当我尝试运行它时。
    • 你好,抱歉。如果您使用 .setValues(values) 它将完成这项工作。我相应地编辑了我的答案。
    • 我可以使用我在你的“if”语句中找到的一些代码,现在我有了一个完整的工作部分。感谢您的帮助!
    • @TacoSteve 很高兴听到这个消息。在这种情况下,如果您能将我的答案标记为已接受,我将不胜感激。
    【解决方案2】:

    经过更多研究,我收集了更多代码,现在运行良好。

    function CopyDataToNewFile(targetSheetName,targetSsId, sourceSheetName,sourceSsId) {
      var ss = SpreadsheetApp.openById('abcd1234sydfhnsgb').getSheetByName('Timesheet');
      var ssd = SpreadsheetApp.openById('abcd1234srymsnzd').getSheetByName('Info');
    
      var therapist = ss.getRange('D6:K6').getValues();
      var sourceData10 = ss.getRange('A10:10').getValues();  
      ssd.getRange(ssd.getLastRow()+1,1,therapist.length,therapist[0].length).setValues(therapist);
    
      if (!ss.getRange(10, 1).isBlank()) {
        ssd.getRange(ssd.getLastRow()+1,1,sourceData10.length,sourceData10[0].length).setValues(sourceData10);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2021-03-28
      • 2018-12-20
      • 1970-01-01
      相关资源
      最近更新 更多