如何使用谷歌应用程序脚本从一个谷歌电子表格复制到另一个谷歌电子表格? [英] How to copy a row from one google spreadsheet to another google spreadsheet using google apps script?

查看:125
本文介绍了如何使用谷歌应用程序脚本从一个谷歌电子表格复制到另一个谷歌电子表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我想从一个电子表格复制一个特定的行到另一个电子表格,使用谷歌应用脚​​本。任何人都可以帮助我得到答案。

解决方案

请查看此处的文档:

http://code.google.com/googleapps/appsscript/service_spreadsheet.html



我们假设您正在工作在您复制的电子表格中。



您必须获取当前电子表格和目标电子表格的句柄。您需要获取目标电子表格的ID。详细信息在那里链接。

  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var target = SpreadsheetApp.openById(abc1234567);

接下来,我们需要选取这些电子表格中的特定工作表。假设您的行位于名为New Stuff的工作表中,并且您在名为Archive的目标电子表格中有一张工作表。

  var source_sheet = ss.getSheetByName(New Stuff); 
var target_sheet = target.getSheetByName(Archive);

现在,Google应用程式电子表格所使用的概念是范围。范围只是一大块细胞。所以我们需要确定从范围和范围。

  var source_range = source_sheet.getRange(A10:G10 ); 
var target_range = target_sheet.getRange(A1:G1);

因此,我们将取这一行并将其放在目标工作表的第一行。现在为实际副本:

  source_range.copyTo(target_range); 

你完成了!



现在,这总是会打破目标工作表的第一行。你可以做很多事情来阻止它。而不是总是使用第一行,您可以使用图表对象方法找到最后一行,然后添加一行,然后将其用作范围。

  var last_row = target_sheet.getLastRow(); 
target_sheet.insertRowAfter(last_row);
var target_range = target_sheet.getRange(A+(last_row + 1)+:G+(last_row + 1));

这样每次复制一行时,它就会被添加到工作表的底部。

Hello I wanted to copy a particular row from one spreadsheet to another spreadsheet using google apps script.Can anyone please help me to get the answer for this.

解决方案

Check out the documentation here:

http://code.google.com/googleapps/appsscript/service_spreadsheet.html

Let's assume you're working in the spreadsheet where you're copying from.

You'd have to get a handle to the current spreadsheet and the target spreadsheet. You'll need to get the ID for the target spreadsheet. Details are in the link up there.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("abc1234567");

Next we need to pick the particular sheets within those spreadsheets. Let's say your row is on the sheet named "New Stuff", and you have a sheet in the target spreadsheet named "Archive".

var source_sheet = ss.getSheetByName("New Stuff");
var target_sheet = target.getSheetByName("Archive");

Now, the concept that google apps spreadsheets use are ranges. A range is just a chunk of cells. So we need to determine the from-range and the to-range. Let's say your sheet has 7 columns and you want the 10th row.

var source_range = source_sheet.getRange("A10:G10");
var target_range = target_sheet.getRange("A1:G1");

So we're going to take that row and put it on the first row of the target sheet. Now for the actual copy:

source_range.copyTo(target_range);

And you're done!

Now, this will always clobber the first row of the target sheet. There's plenty you can do to stop that. Instead of always using the first line, you could use the sheet object methods to find the last row, add one after, and then use it as your range.

var last_row = target_sheet.getLastRow();
target_sheet.insertRowAfter(last_row);
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));

That way each time you copy a row over, it just gets added to the bottom of the sheet.

这篇关于如何使用谷歌应用程序脚本从一个谷歌电子表格复制到另一个谷歌电子表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆