2017-02-10 50 views
0

我試圖從第8列將字「lead」複製到具有不同ID的另一個電子表格,我根本沒有得到任何結果。這是我迄今能夠做到的。我找不到代碼的錯誤。你們能幫我找到嗎?按ID複製行到另一個谷歌工作表 - 不工作

function onEdit(event) { 
    // assumes source data in sheet named Needed 
    // target sheet of move to named Acquired 
    // test column with lead is col 8 or H 
    // target sheet Id is 1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0 
    // sheet name of target Id is Sheet1 
    var ss = SpreadsheetApp.openById("1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0"); 
    var s = event.source.getActiveSheet(); 
    var r = event.source.getActiveRange(); 

    if(s.getName() == "List" && r.getColumn() == 8 && r.getValue() == "lead") { 
    var row = r.getRow(); 
    var numColumns = s.getLastColumn(); 
    var targetSheet = ss.getSheetByName("Sheet1"); 
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); 
    s.getRange(row, 1, 1, numColumns).copyTo(target); 

    } 
} 
+0

該代碼適用於我,想知道您的腳本是否有權訪問電子表格? –

+0

同樣在這裏。檢查執行記錄以獲取有用的信息。 https://developers.google.com/apps-script/troubleshooting#execution_transcriptE – k4k4sh1

+0

它給了我這個錯誤「執行失敗:目標範圍和源範圍必須在同一電子表格上。」 –

回答

1

使用range.setValues()代替copyTo。 請注意,格式不被複制,只有值。 我改變了範圍的尺寸與目標範圍的尺寸相匹配。

function onEdit(event) { 
    // assumes source data in sheet named Needed 
    // target sheet of move to named Acquired 
    // test column with lead is col 8 or H 
    // target sheet Id is 1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0 
    // sheet name of target Id is Sheet1 
    var ss = SpreadsheetApp.openById("1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0"); 
    var s = event.source.getActiveSheet(); 
    var r = event.source.getActiveRange(); 

    if(s.getName() == "List" && r.getColumn() == 8 && r.getValue() == "lead") { 
    var row = r.getRow(); 
    var numColumns = s.getLastColumn(); 
    var targetSheet = ss.getSheetByName("Sheet1"); 
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns); 
    target.setValues(s.getRange(row, 1, 1, numColumns).getValues()); 
    } 
} 
相關問題