[英]How to copy a row from one google spreadsheet to another google spreadsheet using google apps script?
[英]CopyTo: How to push data/copy a cell from one google spreadsheet to another google spreadsheet using google apps script?
我正在寻找一种解决方案,用于将源电子表格中的特定单元格值从“总计”选项卡复制到另一个电子表格,再到“目标电子表格”选项卡“全部复制”。 每当我更改单元格G10的值时,它都应该起作用。 在我向G10输入任何值之前,都可以访问目标表。
(“源”选项卡名称是“ Sum all:G10”-表格包含10个不同的选项卡)
(目标选项卡名称为“ CopyData:T12”-工作表有10个不同的选项卡)
使用Google Apps中内置的IMPORTRANGE()
函数:
在目标单元格中,键入以下公式
=IMPORTRANGE("FILE_ID_HERE","Sum all!G10:G10")
该函数的语法是
=IMPORTRANGE("FILE_ID","SHEET_NAME!RANGE_START:RANGE_END")
首次键入此函数时,单元格中将出现错误。 只需单击它,然后选择“允许”将两张表链接在一起。 即使是相同的电子表格,也会发生此错误。 只要您具有对两个电子表格的编辑权限,此功能也可以链接两个单独的电子表格。
我假设从您的问题出发,您要在同一电子表格文档之间来回复制值,但要在电子表格的不同工作表上的不同单元格之间进行复制。 Google Apps Script API将标签称为“表格”,将整个文档称为“电子表格”。
首先,打开脚本编辑器
其次,为onEdit
创建一个函数。
制作名为onEdit
的函数将创建一个函数,该函数使用无授权的“简单触发器”在每次触发edit
触发器时运行。 每当用户编辑单元格时,Google表格就会自动发送此事件。 该函数的参数e
是触发器传递的事件。
function onEdit(e) {
// Get the sheet named "Sum all" from the active spreadsheet (i.e. the one you are editing)
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sum all");
// Returns the active cell
var cell = source .getActiveCell();
// Compare to see if its the right cell you're looking for
// getRow and getColumn methods return integers for the row and column of the cell
// A = 1, B = 2, ... G = 7
if (cell.getRow() == 10 && cell.getColumn() == 7) {
// If its the right cell, copy to the other cell
var target = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyData");
// set the value of the desired cell in the target sheet
target.getRange("T12").setValue(cell.getValue());
}
}
三,保存脚本
保存脚本,重新加载文件,然后进行测试。
如果您的标签位于不同的电子表格中
更改此行:
var target = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyData");
对此:
var target = SpreadsheetApp.openById("FILE_ID").getSheetByName("CopyData");
并在我已写FILE_ID
位置插入目标电子表格的文件ID。
您还需要使用“已安装的触发器”,因为简单的触发器无法打开远程电子表格。 为此,请更改名称(因此它不再是简单的触发器功能),然后按照此处的步骤进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.