我有一張表,其中包含通過 IMPORTRANGE 匯入到另一張表的資料。當我對電子表格 1 >>> 電子表格 2 進行更改時,我有一個腳本可以將資料從復制復制到粘貼。一切正常,但是我想確保腳本僅在“復制”表中進行更改而不是其他任何更改時運行。目前,它獨立于我所做的更改。
電子表格 1 電子表格2
我嘗試了 onChange 觸發兩種不同的方式...這種方式進行更改,但在任何作業表中進行更改時觸發
function Trigger2(e) {
var sheet = e.source.getActiveSheet();
if (sheet.getName() === 'Copy') {
copyInfo();
}
}
和(這不起作用)
function Trigger2(e) {
var sheet = e.source.getActiveSheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
if( sheet.getActiveSheet().getName() !== "Copy" ) return;{
copyInfo();
}}
復制代碼看起來像這樣......
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,1,12,8);
// get destination range
var destination = pasteSheet.getRange(2,1,12,8);
// copy values to destination range
source.copyTo(destination);
}
uj5u.com熱心網友回復:
匯入范圍更改后將更改從一張表復制到另一張表
電子表格 2 從電子表格 1 表格 1 向表格 1 匯入一個范圍,當電子表格 2 表格 1 發生更改時,我們將資料從表格 2 表格 1 復制到電子表格 2 表格 2。
function onMyChange(e) {
//Logger.log(JSON.stringify(e));//useful during setup
//e.source.toast("Entry");//useful during setup
const sh = e.source.getSheetByName("Sheet1")
Logger.log(sh.getName());
if(e.changeType == "OTHER") {
let tsh = e.source.getSheetByName("Sheet2");
sh.getRange(2,1,12,8).copyTo(tsh.getRange(2,1));//Only need the upper left hand corner of the range. Thus you change change the size of the source data without having to change the target range.
}
}
不幸的是,人們可能傾向于嘗試使用 e.source.getActiveSheet(),除非在 SpreadsheetApp.getActive().getSheets()[0] 中,否則不需要獲取您撰寫的作業表。在這種情況下,onChange 事件物件始終回傳電子表格中最左側的作業表,因此它不能用于識別正在從 importRange 函式寫入的作業表。
使用此功能,您不再需要其他功能。
uj5u.com熱心網友回復:
一個簡單的if應該可以作業,但是您試圖將名稱與錯誤的欄位進行比較。根據 Google 的檔案,sourceEvent Object 中的欄位e回傳的是Spreadsheet物件,也就是整個檔案。
相反,您可以使用該e.range欄位,其中包含進行編輯的確切范圍,并且Range該類具有getSheet()獲取父作業表的方法。
因此,您可以將示例修改為以下內容:
function Trigger2(e) {
var sheet = e.range.getSheet();
if (sheet.getName() === 'Copy') {//Edit: this will only work if sheet is the most left sheet in the spreadsheet
copyInfo();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446578.html
上一篇:兩列中的條件格式
