大家好
我有 2 张谷歌表格,
Source
和 Destination
。我想使用 google apps 脚本将一系列数据从源表复制到目标表。因此,数据应出现在“目标”表的最后一行。这是我的代码:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Source");
var pasteSheet = ss.getSheetByName("Destination");
// get source range
var source = copySheet.getRange(3,1,1,10);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
// copy values to destination range
source.copyTo(destination);
}
似乎脚本只允许我在一个谷歌工作表(不同的选项卡)而不是两个不同的谷歌工作表中进行复制。我可以知道应该如何修改它,以便脚本能够找到我的目标谷歌工作表并将数据粘贴到最后一行吗?任何帮助将不胜感激!
我尝试使用
onEdit
来触发脚本,但似乎不起作用。以下是我的脚本:
function onEdit(e) {
if (e.range.columnStart == 11 && e.range.rowStart == 3){
var ss = SpreadsheetApp.getActiveSpreadsheet();
const id ='1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw';
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = SpreadsheetApp.openById(id).getSheetByName("Sheet1");
if (e.value == 'Submit'){
var source = copySheet.getRange(3,1,1,10);
const values = source.getValues();
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
destination.setValues(values);
}
}
}
来源: https://docs.google.com/spreadsheets/d/1jnOvE-Dc7y9o7GsgN9fjOXZWlUCwiJayugX5q4Y3CA0/edit#gid=0
目的地: https://docs.google.com/spreadsheets/d/1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw/edit#gid=0
使用方法 Range.getValues() 和 Range.setValues()
const id ='ID of paste Spreadsheet here';
var pasteSheet = SpreadsheetApp.openById(id).getSheetByName("Destination");
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
const values = source.getValues();
destination.setValues(values);
方法
getSheetByName
用于定义电子表格中的工作表。要定义特定电子表格,您应该使用 openById
并关联电子表格 ID (https://docs.google.com/spreadsheets/d/{spreadsheet ID}/edit)。
将源表定义为:
var copysheet = SpreadsheetApp.openById('enter id for Source').getSheetByName('Sheet 1')
将您的目标表定义为:
var pastesheet = SpreadsheetApp.openById('enter id for Destination').getSheetByName('Sheet 1')
如果我理解正确,您的目标如下:
/* Every time a user sets the value of cell K3 in source spreadsheet
to "submit". Copy all the values of row 3 and paste them into the
destination sheet under the last row. Then delete the source values
and set the value of K3 to "Clear" again. */
您需要安装一个触发器和一个在触发时复制值的函数。所以让我们一步一步来做吧。
copyInfo()
函数,以便它将目标电子表格 ID 作为参数。function copyTo(destinationSSId) {
// Open source SS at tab named "Source"
let copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source");
// Open destination SS at tab named "Destination"
let pasteSheet = SpreadsheetApp.openById(destinationSSId).getSheetByName("Destination");
// Get source values
let sourceValues = copySheet.getRange(3, 1, 1, 10).getValues();
// Set values to destination range
pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, 1, 10).setValues(sourceValues);
// Log the pasted values from the last row
console.log("Last row pasted: " + pasteSheet.getRange(pasteSheet.getLastRow(), 1, 1, 10).getValues()[0]);
}
function clearSubmission() {
let copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source");
copySheet.getRange(3, 1, 1, 10).clearContent()
copySheet.getRange(3, 11, 1, 1).setValue("Clear");
console.log("Source submitted and cleared.")
}
function myTriggeredFunction(e) {
console.log(JSON.stringify(e.range), e.value)
if (e.range.columnStart == 11 && e.range.rowStart == 3) {
if (e.value == 'Submit') {
let destinationSSId = '{YOUR DESTINATION SS ID}';
copyTo(destinationSSId);
clearSubmission();
}
}
}
myTriggeredFunction
。非常感谢..您的回复激励我为 onEdit() 找到解决方案