0
votes

I have a script that will copy and send a range of data to another sheet. I want to add another part to the script, that will perform the same function but append the data to the next blank row.

function saveToRecords() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var archive = ss.getSheetByName("Archive");
    var source = ss.getRange('Work!A10:R');
    var lastRow = archive.getLastRow();
    source.copyTo(ss.getRange('Archive!A1:R1'), {contentsOnly: true});
}

I've been following this question for guidance, but when I attempt to implement the solution, I keep receiving a mismatched range error.

2

2 Answers

1
votes
function saveToRecords() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var archive = ss.getSheetByName("Archive");
  var work = ss.getSheetByName('Work');
  var source = work.getRange(10,1,work.getLastRow()-9,18);
  source.copyTo(archive.getRange(archive.getLastRow()+1,1));
}
1
votes

Ended up going with something like this

function saveToRecords() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Work'), true);
  spreadsheet.getRange('A10:R').activate();
  var source_sheet = spreadsheet.getSheetByName("Work");
  var target_sheet = spreadsheet.getSheetByName("Archive");
  var source_range = source_sheet.getActiveRange();
  var last_row = target_sheet.getLastRow();
  var values = source_range.getValues();
  target_sheet.getRange(last_row + 1, 1, values.length, values[0].length).setValues(values);
  spreadsheet.getRange('A2').activate();
}

This adds my A10:R range and pastes values into the first available row in my Archive sheet