简体   繁体   English

使用Google Apps脚本自动将行复制到新工作表

[英]Copy rows automatically to new sheet with Google Apps Script

I have a spreadsheet with some information on, and I want it to copy across to a second sheet automatically. 我有一个包含一些信息的电子表格,我希望它可以自动复制到第二张纸上。 I also want it to copy across automatically when I insert new rows within the rows copied across. 当我在复制的行中插入新行时,我还希望它自动复制。 I found the code below but I don't completely understand what I would have to do to it for it to do what I want, or if I would need to add anything. 我在下面找到了代码,但是我不完全了解要执行它想要执行的操作,或者是否需要添加任何内容,我将需要对它做些什么。 I have basic coding knowledge but not much, so if someone could explain this it would be much appreciated. 我具有基本的编码知识,但知识不是很多,因此,如果有人可以解释这一点,将不胜感激。

function myFunction() {
var source = SpreadsheetApp.openById('xxxxxx');
var sourcesheet = source.getSheetByName('sheet1');
var target = SpreadsheetApp.openById('xxxxx')
var targetsheet = target.getSheetByName('sheet1');
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetSheet.appendRow(rangeValues); 
}

You can do it much more simply by using the =IMPORTRANGE() function, eg =IMPORTRANGE("https://docs.google.com/spreadsheets/d/10X7KGGFd4U8IwQO4QgunmLMphtwwEF9bGwfu3Vrm3AM/","Sheet1!A:Z") 您可以使用= IMPORTRANGE()函数来轻松完成此操作,例如=IMPORTRANGE("https://docs.google.com/spreadsheets/d/10X7KGGFd4U8IwQO4QgunmLMphtwwEF9bGwfu3Vrm3AM/","Sheet1!A:Z")

Put this formula in the second spreadsheet, replace the URL with the URL of the first spreadsheet (I just put an example one) and tweak the "sheet_name!range" with what you need. 将此公式放在第二个电子表格中,将URL替换为第一个电子表格的URL(我只是举一个例子),然后根据需要调整“ sheet_name!range”。 The 2nd spreadsheet will then automatically update whenever the inputted range of the 1st spreadsheet changes :) 输入的第一个电子表格的范围发生变化时,第二个电子表格将自动更新:)

Hope this helps! 希望这可以帮助!

If you don't completely understand the code that you are using for these, then you can check the documentation of Google Apps Script for Spreadsheet to know the use and purpose of different classes and methods. 如果您不完全了解用于这些代码的代码,则可以查看Google Apps脚本电子表格文档 ,以了解不同类和方法的用途和目的。

Now, for your question about rows that you need to copy from one sheet to another, I think this tutorial can help you with that. 现在,对于您需要从一张纸复制到另一张纸的行的问题,我认为本教程可以为您提供帮助。 It explains here the step by step procedure that you need to do in your Apps Script. 它在此处说明了您在Apps脚本中需要执行的分步过程。

For more information, check this related SO questions: 有关更多信息,请检查以下相关的SO问题:

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

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