1
votes

I have been looking for a way to copy specific columns and rows of data to an existing sheet. My goal is to create a script that will pull data from the master items sheet and create a ordering form on an existing sheet.

Here's my code

function createOrder() {
var ss = SpreadsheetApp.getActiveSheet(); 
var ordess = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ordering"); 
var mayitems = 186; //number of rows
var descrip = ss.getRange(2, 10, mayitems, 4).getValues(); //first 4 columns to pull
var descrip2 = ss.getRange(2, 19, mayitems).getValues();  //fifth column to pull
var price = ss.getRange(2, 76, mayitems).getValues(); //sixth column to pull
var pallets = ss.getRange(640, 76, 1).getValues(); //specific cell to pull
var targdesc = ordess.getRange(1, 1, mayitems, 4); //first 4 columns to copy to
descrip.copyTo(targdesc); //copying the first 4 columns.
}

I looked at the function reference and copyTo seems to be the one I should be using. I followed the example, but when I run the code, it gave me a TypeError Cannot find function copyTo in object.

I'm also looking to create a script that will filter rows with blank cells like what the filter option can do, but done automatically.

Any help is appreciated.

1

1 Answers

1
votes

Your variable descrip is a two dimensional array. The copyTo() method works on a range, it won't work on an array. It looks like you are getting data from specific columns that are not next to each other. So, you need to either copy multiple ranges in multiple lines of code, or assemble the data that you want into an array, and then use setValues() to set the values in the target location. If you want to use copyTo(), then you don't need to use getValues() at all.

var descrip = ss.getRange(2, 10, mayitems, 4);
var targdesc = ordess.getRange(1, 1, mayitems, 4);
descrip.copyTo(targdesc);

The only difference is, that the getValues() method is not chained.