0

I need to copy one range of 22 columns, 300 rows, into another range of the same size which will be added to another, existing sheet.

When I try to do this I receive the error, "Incorrect range width, was 22 but should be 44". I have been unsuccessful in making the ranges match.

  var sourceRange = ss1.getRange(1, 1, 300, 22);
  var sourceValues = sourceRange.getValues();
  var archiveRange = ss3.getRange(1, ss3.getLastColumn()+1,300,ss3.getLastColumn()+22);
  archiveRange.setValues(sourceValues);

2 Answers 2

1

The method .getLastColumn() returns the integer of the last column that your sheet currently has. If your sheet has 10 columns, .getLastColumn() will return 10.

You have a few potential options that you were asking for. The first, shown below, is if you are looking to append a new range at the right end of your sheet. It is also a wise idea to define variables and call them in the most basic form instead of defining them within your other methods. This practice will reduce errors and clarify your code.

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceRange = ss1.getRange(1, 1, 300, 22);
var sourceValues = sourceRange.getValues();
var ss3 = spreadsheet.getSheetByName("ss3");
var lastColumn = ss3.getLastColumn();
var archiveRange = ss3.getRange(1, lastColumn + 1, 300, 22); //Start at first row, the next column after the last one, for 300 rows and 22 columns.
archiveRange.setValues(sourceValues);

This code above will add 22 columns and 300 rows to the right of your sheet. Your sheet will then stay 300 rows regardless of times you run this however your columns will extend right further and further.

Alternatively, if you are looking to append 300 rows at the bottom of your sheet using the same 22 columns (extend length, NOT width), use this code:

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceRange = ss1.getRange(1, 1, 300, 22);
var sourceValues = sourceRange.getValues();
var ss3 = spreadsheet.getSheetByName("ss3");
var lastColumn = ss3.getLastColumn();
var lastRow = ss3.getLastRow();
var archiveRange = ss3.getRange(lastRow + 1, 1, 300, 22); //Start at the next row after the last, the first column, for 300 rows and 22 columns.
archiveRange.setValues(sourceValues);

This will keep your width 22 rows, and extend your length by 300 rows.

If you are seeking to overwrite your previous data completely so that there exists only 1 archive, use this:

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceRange = ss1.getRange(1, 1, 300, 22);
var sourceValues = sourceRange.getValues();
var ss3 = spreadsheet.getSheetByName("ss3");
var archiveRange = ss3.getRange(1, 1, 300, 22);
archiveRange.setValues(sourceValues);

As you can guess, this keeps the range 300 rows, 22 columns. Your data will overwrite and not add length or width.

You may need to test how effectively and accurately GAS can .getRange() that may not yet exist. If you are seeing errors in the addition of the rows/ columns, it would serve you well to use methods similar to .insertColumnsAfter() and .insertRowsAfter() before you write the values to those new ranges. Documentation on sheet methods can be found here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the information! This should help in many ways.
If this provides an answer or useful information, upvote or accept as the answer for others to find.
0

the formula you are using is .getRange(row, column, numRows, numColumns) so you don't have to write:

var archiveRange = ss3.getRange(1, ss3.getLastColumn()+1,300,ss3.getLastColumn()+22);
  archiveRange.setValues(sourceValues);

but:

var archiveRange = ss3.getRange(1, ss3.getLastColumn()+1,300,22);
  archiveRange.setValues(sourceValues);

1 Comment

Thank you very much for the information! I was reading the last parameter as "column number" and not "number of columns". This fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.