简体   繁体   English

Google脚本将单元格的一部分复制到另一个工作表

[英]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. 我仍然不熟悉Javascript,如果即时通讯出现错误,请原谅。

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." 对于此示例,日期在单元格A1“ 2017年2月28日,上午10:38”中填充。 I want to only copy the date February 28, 2017 and paste it onto another google sheet. 我只想复制日期2017年2月28日,然后将其粘贴到另一个Google工作表中。

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,. 我这样做时收到的错误是:TypeError:在对象February,28中找不到函数copyTo。 (line 13, file "Test") (第13行,“测试”文件)

This maybe something simple i jsut cont figure it out 这可能是一些简单的事情,我想继续弄清楚

You need to use .setValue() to set the value of the range. 您需要使用.setValue()来设置范围的值。 So to set your range to the value in string2 you would replace line 13 with: 因此,要将范围设置为string2中的值,可以将第13行替换为:

target_range.setValue(string2); target_range.setValue(字符串2);

See the Tutorials for some examples of scripts. 有关脚本的一些示例,请参见教程 Especially the Workflows and end-to-end examples. 尤其是工作流程和端到端示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM