0
votes

I am trying to copy data from a sheet (SheetA) to another sheet (SheetB) However, I need the row of the cell to be copied (From Sheet A) to be based off of another cell.

For example: SheetA has data on A1, A2, A3, A4 etc... SheetB has a cell (B1) that a user types in a value. When the script is run the data from SheetA will be copied from column A and a row based off of the value in B1 to SheetB. So if a user types "5" into B1 and runs, the data from A5 will be copied from SheetA to A1 on sheetB

Here is a script that I have so far, how do I modify this correctly to complete the code?

    //Moves data from SheetA -> SheetB;
   SheetA.getRange("?").copyTo(SheetB.getRange("A1"));

What do I have to replace where the '("?")' is?

Thank you!

2
There is a similar question on SO. Please see, Copy values and formatting to... Please see and let us know where you're stuck.Br. Sayan

2 Answers

0
votes

Using setValue() instead will be easier to implement and understand.

Try to implement like below:

Get the value of the input from SheetB

var inputValue = SheetB.getRange(SheetBRow, SheetBColumn).getValue();

-

Use the inputValue as a row parameter for getRange() to get the value of a cell from SheetA

var SheetACellValue = SheetA.getRange(inputValue, SheetAColumn).getValue();

-

Set the value of a cell on SheetB using the value from SheetACellValue

Cell SheetBCell = SheetB.getRange(SheetBRow,SheetBColumn);
SheetBCell.setValue(SheetACellValue);

You can implement this by method chaining. I just divided each for a better view and understanding.

0
votes

Assuming you defined SheetA and SheetB and the copyTo Range is fixed (A1) this is your code (following the syntax in your attempt):

SheetA.getRange('A'+SheetB.getRange('B1').getValue).copyTo(SheetB.getRange('A1'));

or (using setValue instead of copyTo because you are adressing one specific cell):

SheetB.getRange('B1').setValue(SheetA.getRange('A'+SheetB.getRange('B1').getValue));

This is basic knowledge when working with Apps Script and Sheets. I recommend you read up on the SpreadsheetApp Documentation. Moreover this is a duplicate of so many similar questions. Next time search for your question using the google-apps-script tag.