简体   繁体   English

使用 Google Apps 脚本将一个 Google 电子表格中的特定数据复制到另一个 Google 电子表格

[英]Copy Specific Data in one Google Spreadsheet to another Google Spreadsheet with Google Apps Script

I have a spreadsheet that people will fill out and then hit a submit button.我有一个电子表格,人们将填写该电子表格,然后点击提交按钮。 That button creates a PDF and emails that data to people.该按钮创建一个 PDF 并将该数据通过电子邮件发送给人们。 A portion of that data needs to be compiled in another spreadsheet each time someone submits their data.每次有人提交数据时,都需要将一部分数据编译到另一个电子表格中。

I have code that can copy a range and paste it in the spreadsheet.我有可以复制范围并将其粘贴到电子表格中的代码。

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);

}

I have two issues with this code:我对这段代码有两个问题

  1. I cannot select specific cells of data , I need to select J6 but also the entire 11th row.我无法选择特定的数据单元格,我需要选择 J6 以及整个第 11 行。 A bonus would be if it sees a value in say, A14, then it will copy the whole 14th row.如果它在 A14 中看到一个值,那么它就会复制整个第 14 行。 But if nothing is in A14 it will not copy that data.但如果 A14 中没有任何内容,则不会复制该数据。
  2. The data overwrites itself each time data is submitted , I need each new addition of data to just show up under the last one.每次提交数据时,数据都会覆盖自身,我需要每次新添加的数据都显示在最后一个下方。

I have been looking for a solution to this for hours and am not versed in Javascript enough to write anything custom.几个小时以来,我一直在寻找解决方案,但我对 Javascript 的精通程度不足以编写任何自定义内容。 Thank you!谢谢!

In the script below you can see how you can select individual cells or entire rows and how you can copy their contents into another sheet.在下面的脚本中,您可以看到如何选择单个单元格或整行,以及如何将它们的内容复制到另一个工作表中。

There's also an example of how you can check if a cell has a value or not.还有一个示例说明如何检查单元格是否具有值。

You can find a solution here how you can copy stuff into an existing sheet without overwriting what's already in there.您可以在此处找到解决方案如何将内容复制到现有工作表中,而不会覆盖其中已有的内容。

 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 }

EDIT: Use .setValues instead of .copyTo in order to copy entire rows.编辑:使用 .setValues 而不是 .copyTo 来复制整行。

After some more research I gathered some more code this is working beautifully now.经过更多研究,我收集了更多代码,现在运行良好。

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);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 在 Google Apps 脚本中从另一个电子表格编辑一个电子表格 - Editing one Spreadsheet from another Spreadsheet in Google Apps Script 使用 Google Apps 脚本将数据范围从一个 Google 电子表格导出到另一个 - Export Range of Data from One Google Spreadsheet to Another using Google Apps Script 使用Google脚本将数据从一个Google电子表格拉到另一电子表格 - Pull data from one Google spreadsheet to another using Google script 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 谷歌应用程序脚本 html 到电子表格 - Google apps script html to spreadsheet Google Apps脚本:如何从一个电子表格中提取今天输入的数据并将其转换为另一电子表格? - Google Apps Script: How do I pull data entered today from one spreadsheet and transpose it to another? 如何使用Google Apps脚本自动下载Google电子表格数据 - how to automatically download Google spreadsheet data using Google Apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM