簡體   English   中英

使用 Google Apps 腳本將一個 Google 電子表格中的特定數據復制到另一個 Google 電子表格

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

我有一個電子表格,人們將填寫該電子表格,然后點擊提交按鈕。 該按鈕創建一個 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 的精通程度不足以編寫任何自定義內容。 謝謝!

在下面的腳本中,您可以看到如何選擇單個單元格或整行,以及如何將它們的內容復制到另一個工作表中。

還有一個示例說明如何檢查單元格是否具有值。

您可以在此處找到解決方案如何將內容復制到現有工作表中,而不會覆蓋其中已有的內容。

 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 來復制整行。

經過更多研究,我收集了更多代碼,現在運行良好。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM