Question

I have a sheet that will update based on new articles in an RSS feed using importfeed() and the URLs are analyzed with importxml(). Problem is the data is always changing and I would like to archive old data from the importfeed() function. I'd just like to move the most updated data from one sheet to another without overwriting anything.

Here is the sample code from: https://stackoverflow.com/users/650206/jeremy:

function myFunction() {
// Get Spreadsheets
var source = SpreadsheetApp.openById("spreadsheetKeySource");
var target = SpreadsheetApp.openById("spreadsheetKeyTarget");

// Set Sheets
var source_sheet = source.getSheetByName("Sheet1");
var target_sheet = target.getSheetByName("Sheet1");

// Get target last row
var last_row = target_sheet.getLastRow();

// Set Ranges
var source_range = source_sheet.getRange("A1:B1");
var target_range = target_sheet.getRange("A"+(last_row+1)+":B"+(last_row+1));

// Fetch values
var values = source_range.getValues();

// Save to spreadsheet

   target_range.setValues(values);
}

Problem with the above code is it only selects one row when I need it to select an entire range (e.g. A1:G55) and paste that data below the old data in another sheet.

Any help would be much appreciated!

EDIT

The error I'm getting when I change:

var source_range = source_sheet.getRange("A1:B1");
var target_range = target_sheet.getRange("A"+(last_row+1)+":B"+(last_row+1));

to:

var source_range = source_sheet.getRange("A1:G25");
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));

is "Incorrect range height, was 5 but should be 1 (line 21, file "Code")"

which is this piece of code:

target_range.setValues(values);
Was it helpful?

Solution

var source_range = source_sheet.getRange("A1:G25");
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));

The lines above attempt to copy 25 rows of data and paste that into one row. I'm surprised that the error message says "was 5 but should be 1". I'd have expected it to say "was 25 but should be 1".

Try this instead:

var source_range = source_sheet.getRange("A1:G25");
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+25));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top