Capt AwesomE
Capt AwesomE

Reputation: 1

Google Script Copying part of a cell to another sheet

Goal: Trying to copy part of some text from one cell on a spread sheet and pasting it on to another one.

I am still new to Javascript to please forgive if im going about this all wrong.

So on one spread sheet some information gets populated automatically from a service I use. I want to take part of that information and copy and paste it onto another spread sheet.

For this example the date gets populated in the cell A1 "February 28, 2017 at 10:38AM." I want to only copy the date February 28, 2017 and paste it onto another google sheet.

This is what my current code looks like:

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var target = SpreadsheetApp.openById("abc1234");
 var source_sheet = ss.getSheetByName("Text");
 var target_sheet = target.getSheetByName("Bills"); 
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange("A1:D4");
 var cell = range.getCell(1, 1);
 var string1 = cell.getValue();
 var string2 = string1.split(" ", 2);
 var target_range = target_sheet.getRange("J4");

 string2.copyTo(target_range);

 //Logger.log(string2);

}

The error I receive when i do this is: TypeError: Cannot find function copyTo in object February,28,. (line 13, file "Test")

This maybe something simple i jsut cont figure it out

Upvotes: 0

Views: 479

Answers (1)

Karl_S
Karl_S

Reputation: 3574

You need to use .setValue() to set the value of the range. So to set your range to the value in string2 you would replace line 13 with:

target_range.setValue(string2);

See the Tutorials for some examples of scripts. Especially the Workflows and end-to-end examples.

Upvotes: 0

Related Questions