如果记录不存在,Google表格会将某些字段复制到新表格中

时间:2019-05-08 10:28:57

标签: google-sheets copy google-sheets-formula google-sheets-query

如果该记录在工作表2中尚不存在,我正在尝试自动将工作表1中的记录添加到工作表2中。

第1页有20列,但是我只需要将A,D,E和F列复制到第2页上

列A是会员编号,这是我要用来验证记录是否已存在于工作表2中以开始复制的字段。

我的编码技能非常有限,因此我正在寻求这个很棒的社区的帮助。

Sheet1

+------+----------+-------+------+------------+-----------+
| ID   | Date     | Time  | Sal. | First Name | Last Name |
+------+----------+-------+------+------------+-----------+
| 1234 | 05/12/19 | 16:00 | Mr.  | John       | Doe       |
| 0001 | 05/12/19 | 17:00 | Mrs. | Jennifer   | Lawrence  |
| 1234 | 05/13/19 | 16:00 | Mr.  | John       | Doe       |
| 1233 | 05/13/19 | 16:00 | Mr.  | Johnny     | Deep      |
+------+----------+-------+------+------------+-----------+

Sheet2

+------+------+------------+-----------+
|  ID  | Sal. | First Name | Last Name |
+------+------+------------+-----------+
| 1234 | Mr.  | John       | Doe       |
| 0001 | Mrs. | Jennifer   | Lawrence  |
| 1233 | Mr.  | Johnny     | Deep      |
+------+------+------------+-----------+

这是我尝试过的代码。它仅复制第一列。我如何也复制第4、5和6列?

function copy_Guest_info() {
  var spreadsheet, sheet, sheet2, endrow, 
endcol,endrow2,endcol2,data,data2,resultArray,n,l,l2,p,doesNotExist,
  sheetOneValue, numberOfMissingValues, innerArray,i,thisValue;

  spreadsheet = 
SpreadsheetApp.openById('1L3XjJvgruB_JNZWd2Vo2Tz32cc3quzhlIjSaPkOvV10');

  sheet = spreadsheet.getSheetByName("Reservations");
  sheet2 = spreadsheet.getSheetByName("LCAH Guest Profile");

  endrow = sheet.getLastRow();
  endcol = sheet.getLastColumn();
  endrow2 = sheet2.getLastRow();
  endcol2 = sheet2.getLastColumn();

  data = sheet.getRange(1, 1, endrow, 1).getValues();
  //getRange(start row, start column, number of Rows, number of Columns)
  data2 = sheet2.getRange(1, 1, endrow2, 1).getValues();
  data2 = data2.toString().split(","); //Flatten the 2D array to one D

  resultArray = [];
  innerArray = [];
  l = data.length;
  l2 = data2.length;

  for (n=0;n<l;n+=1) {
    sheetOneValue = data[n][0];
    innerArray = []; //Reset

    for (p=0;p<l2;p+=1) {
      doesNotExist = data2.indexOf(sheetOneValue) === -1;//If the value is 
NOT found, indexOf returns -1  
      if (!doesNotExist) break; //If it exists in the list, no need to go 
any further
      if (doesNotExist) {//If the value in the cell from data one does NOT 
exist in data two, add it to the array

        innerArray.push(sheetOneValue)
        resultArray.push(innerArray);
        Logger.log('sheetOneValue: ' + sheetOneValue);
        break ;
      };
    };
  };

  endrow2 = 0;
  for (i=0;i<l;i+=1) {
    thisValue = data2[i];
    if (thisValue==="" || thisValue=== undefined) {//There is nothing in 
this cell
      continue;
    };
    endrow2+=1;
  };
  //Append the data in the array to the bottom of the data two list
  sheet2.getRange(endrow2+1, 1,resultArray.length,1).setValues(resultArray);

  Logger.log(resultArray);  
};

1 个答案:

答案 0 :(得分:0)

也许可以在Sheet2中尝试以下简单公式:

=QUERY('Sheet1'!A:F); "select A,D,E,F"; 0)