将行和粘贴值复制到另一个工作表

时间:2018-04-30 00:30:04

标签: google-apps-script google-sheets copy-paste

我有一个独特的Google表单文档,下面的代码允许我:

  • 将工作表MEL Micro Set Up中的新行复制到Backup Set Up
  • 将工作表MEL Micro Confirmation中的新行复制到Backup Confirmation

一切正常,除非我从两个源文件中删除行,否则会删除备份文件中的行。

我们怎样才能避免这种情况?

function Copy() {
 var sourceSheet1 = SpreadsheetApp.openById('17ekZcvPQYrcX1ny-RtPQczEfbmd1JZUkRVx6N7qTtCA').getSheetByName('MEL Micro Set Up');
 var range1 = sourceSheet1.getRange(1, 1, sourceSheet1.getLastRow(), sourceSheet1.getLastColumn()).getA1Notation();
 var sourceSheetValues1 = sourceSheet1.getRange(range1).getValues();
 var destinationSheet1 = SpreadsheetApp.openById('17ekZcvPQYrcX1ny-RtPQczEfbmd1JZUkRVx6N7qTtCA').getSheetByName('Backup Set Up');
 destinationSheet1.getRange(range1).setValues(sourceSheetValues1);

 var sourceSheet2 = SpreadsheetApp.openById('17ekZcvPQYrcX1ny-RtPQczEfbmd1JZUkRVx6N7qTtCA').getSheetByName('MEL Micro Confirmation');
 var range2 = sourceSheet2.getRange(1, 1, sourceSheet2.getLastRow(), sourceSheet2.getLastColumn()).getA1Notation();
 var sourceSheetValues2 = sourceSheet2.getRange(range2).getValues();
 var destinationSheet2 = SpreadsheetApp.openById('17ekZcvPQYrcX1ny-RtPQczEfbmd1JZUkRVx6N7qTtCA').getSheetByName('Backup Confirmation');
 destinationSheet2.getRange(range2).setValues(sourceSheetValues2);

if (sourceSheet1.getName() !== 'MEL Micro Set Up' || range1.columnStart !== 11 || range1.value !== 0 ) return;
destinationSheet1.getSheetByName('Backup Set Up')
    .appendRow(sourceSheet1.range.offset(0, -3, 1, 11)
        .getValues()[0]);
sheet.deleteRow(sourceSheet1.range.rowStart);

if (sourceSheet2.getName() !== 'MEL Micro Confirmation' || range2.columnStart !== 11 || range2.value !== 0 ) return;
destinationSheet2.getSheetByName('Backup Confirmation')
                 .appendRow(sourceSheet2.range.offset(0, -3, 1, 11)
                                        .getValues()[0]);
sheet.deleteRow(sourceSheet2.range.rowStart);
}

0 个答案:

没有答案