使用脚本将Google电子表格列从一个电子表格复制到另一个电子表格 [英] Copying google spreadsheet columns from one spreadsheet to another with a script

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

问题描述

我希望能够将电子表格中的一个柱子(井3)复制到另一个电子表格(谷歌文档电子表格)。我还希望有一些触发器可以查找修改并自动将它们复制到新的电子表格中。



这里是我的代码,那不是加工!谢谢!

 函数copytest()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById(0AvxaM_p22RvwdHl2MFl4MkhteVhmVzFDY2FOblNpVEE);
var source_sheet = ss.getSheetByName(Sheet1);
var target_sheet = target.getSheetByName(Sheet1);
var source_range = source_sheet.getRange(A1:B2);

source_range.copyTo(A1:B2);


$ / code>

我认为它的所有工作除了最后一行source_range.copyTo ( A1:B2);
我是这个脚本业务的新手,非常感谢您的帮助。 copyTo()方法仅适用于目标位于相同的电子表格。
要将数据从一个电子表格复制到另一个电子表格,您必须从源范围中将值(以及任何格式,颜色,字体大小...)转换为变量(二维数组或者在教程中指出的Srik指的是)并将其写回目标范围。例如:



  function copytest()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById(0AnqSFd3iikE3dEpHUGJod2xwbXRqU25wS25HWF9pdEE);
var source_sheet = ss.getSheetByName(Sheet1);
var target_sheet = target.getSheetByName(Sheet1);
var source_range = source_sheet.getRange(A1:B2);
var target_range = target_sheet.getRange(A1:B2);

var values = source_range.getValues();
var bGcolors = source_range.getBackgrounds();
var colors = source_range.getFontColors();
var fontSizes = source_range.getFontSizes();
//利用自动完成来查找其他方法...
target_range.setValues(values);
target_range.setBackgrounds(bGcolors);
target_range.setFontColors(colors);
target_range.setFontSizes(fontSizes);

新的更新代码如下(由问题作者编辑)

 函数copyTest()
{

//获取并格式化数据范围,从床单到床单。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById(idspispopd1234567890);
var source_sheet = ss.getSheetByName(one);
var target_sheet = target.getSheetByName(Sheet1);
var source_range = source_sheet.getRange(A2:A);
var target_range = target_sheet.getRange(A2:A);

//然后写入设置中定义的值
var values = source_range.getValues();
target_range.setValues(values);

//用于检查数据在单元格和格式范围中的最后一行
var lastRow = source_sheet.getLastRow();

//对于邮件
var emailAddress =test@test.test;
var subject =蒙台梭利社区学校指南针导入信息;
var message = values;
MailApp.sendEmail(emailAddress,subject,message);

}






编辑2(BY ANSWERER)



获得一个完整的列,改变范围定义像这样:

  var source_range = source_sheet.getRange(A2:A); 
var values = source_range.getValues();
var target_range = target_sheet.getRange(2,1,values.length,values [0] .length); //使用数组大小​​来定义范围,因为目标列A在此处不一定是相同的大小时间。如果您想要复制多个连续的列,只需相应地定义源表单,在A1表示法列A + B中, B + C将会是 .getRange(A2:C);。



尝试使用整数定义对于范围来说,IMO更容易和更易读:例如,一列满列A + B + C将是 sh.getRange(1,1,sh.getLastRow(),3)

对于不连续的列,它变得有点复杂了....也许是一个新的线程?


I'd like to be able to copy a colum (well 3) from a spreadsheet to another spreadsheet (google docs spreadsheets). I'd also like there to be some sort of trigger that looks for modifications and automatically copies them to the new spreadsheet.

here is the code I have, that is not working! Thanks!

function copytest()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0AvxaM_p22RvwdHl2MFl4MkhteVhmVzFDY2FOblNpVEE");
  var source_sheet = ss.getSheetByName("Sheet1");
  var target_sheet = target.getSheetByName("Sheet1");
  var source_range = source_sheet.getRange("A1:B2");

  source_range.copyTo("A1:B2");

}

I think its all working except the last line source_range.copyTo("A1:B2"); I'm new to this scripting business, so thanks for your help.

解决方案

the copyTo() method works only when destination is in the same spreadsheet. To copy data from one spreadsheet to another you will have to get the values (and whatever format, colors, fontsize...) from the source range into a variable (a 2D array or as indicated in the tutorial that Srik was referring to) and write it back to the target range.

for example :

function copytest()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0AnqSFd3iikE3dEpHUGJod2xwbXRqU25wS25HWF9pdEE");
  var source_sheet = ss.getSheetByName("Sheet1");
  var target_sheet = target.getSheetByName("Sheet1");
  var source_range = source_sheet.getRange("A1:B2");
  var target_range = target_sheet.getRange("A1:B2");

  var values = source_range.getValues();
  var bGcolors = source_range.getBackgrounds();
  var colors = source_range.getFontColors();
  var fontSizes = source_range.getFontSizes();
  // make use of autocomplete to find other methods...
  target_range.setValues(values);
  target_range.setBackgrounds(bGcolors);
  target_range.setFontColors(colors);
  target_range.setFontSizes(fontSizes);
}

NEW UPDATED CODE IS AS FOLLOWS (EDIT BY QUESTION AUTHOR)

function copyTest()
{

//gets and formats data ranges, to and from sheets.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("idspispopd1234567890");
var source_sheet = ss.getSheetByName("one");
var target_sheet = target.getSheetByName("Sheet1");
var source_range = source_sheet.getRange("A2:A");
var target_range = target_sheet.getRange("A2:A");

//gets then writes values defined in setup
var values = source_range.getValues();
target_range.setValues(values);

//for checking last row with data in cell and formatting range
var lastRow = source_sheet.getLastRow();  

//FOR MAIL
var emailAddress = "test@test.test";
var subject = "Compass import information from Montessori Community School";
var message = values;
MailApp.sendEmail(emailAddress, subject, message);

}


EDIT 2 (BY ANSWERER)

to get a full column, change the range definition like this :

var source_range = source_sheet.getRange("A2:A");
var values = source_range.getValues();
var target_range = target_sheet.getRange(2,1,values.length,values[0].length);// use the array size to define the range because the target column A is not necessarily the same size at this time.

if you want to copy multiple contiguous columns just define the source sheet accordingly, in A1 notation column A+B+C would be .getRange("A2:C");.

Try to use the integer definition for range, IMO it's more easy and more readable : for example a range of full columns A+B+C would be sh.getRange(1,1,sh.getLastRow(),3)

For non contiguous columns it's becoming a bit more complicated.... maybe a new thread ?

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

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