Jody Eckert
Jody Eckert

Reputation: 11

Copy and Paste Scrip

I am trying to copy data from COPY SHEET to PASTE SHEET and once complete I want to data in the copy sheet to clear.

COPY SHEET INFORMATION Information is in Column B Priority A - High
First Name New Test
Last Name test
Company JLC Phone Number 3065551212
Email [email protected]
Notes Likes Coffee
Call Back Date 3/7/2022
Date Entered 3/26/2023
Address 679 Rink Ave, Regina SK S4X2P3

PASTE SHEET Information needs to be put into columns A-L

First Name Last Name Company Phone Number Email Notes Call Back Date Date Entered Address

function copyInfo() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Contact Form");
  var pasteSheet = ss.getSheetByName("CRM");

  // get source range
  var source = copySheet.getRange(B1:B10);
  // get destination range
  var destination = pasteSheet.getRange(A2:L2);

  // copy values to destination range
  source.copyTo(destination);

  // clear source values
  source.clearContent();
}

Upvotes: 0

Views: 29

Answers (1)

Tanaike
Tanaike

Reputation: 201358

Modification points:

  • In your script, B1:B10 and A2:L2 of var source = copySheet.getRange(B1:B10); and var destination = pasteSheet.getRange(A2:L2); are required to be set as the string.
  • From your question, it seems that you wanted to copy the values of "B1:B10" to "A2:L2". In this case, it is required to transpose the values when the values are copied.

When these points are reflected in your script, it becomes as follows.

Modified script:

function copyInfo() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Contact Form");
  var pasteSheet = ss.getSheetByName("CRM");

  // get source range
  var source = copySheet.getRange("B1:B10");

  // get destination range
  var destination = pasteSheet.getRange("A2:L2");

  // copy values to destination range
  source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);

  // clear source values
  source.clearContent();
}
  • When this script is run, the values of "B1:B10" of "Contact Form" is copied to "A2:L2" of "CRM". And, "B1:B10" of "Contact Form" is cleared.

  • For example, if you want to append the values to the destination sheet, please modify var destination = pasteSheet.getRange("A2:L2"); as follows.

      var lastRow = pasteSheet.getLastRow() + 1;
      var destination = pasteSheet.getRange(lastRow == 1 ? 2 : lastRow, 1);
    

References:

Upvotes: 0

Related Questions