2016-12-20 55 views
0

我試圖獲得表單2中列備註的值,並將其置於表單1的註釋中。要做到這一點,他們應該有同列公司價值。當我在公司(工作表2)中輸入一個值時,代碼會發現它在公司(工作表1)中是否具有相同的值。如果代碼找到相同的公司價值,則我輸入的註釋(表2)中的值將自動放入註釋(表1)中。我在代碼中使用了if/else語句,但它沒有運行。使用谷歌應用程序腳本自動將單元格的值從表單2複製到表單1

任何幫助將不勝感激。

例:

表1 enter image description here

表2 enter image description here

這裏是我的代碼:)

function onEdit() { 
 
    var sheetId = SpreadsheetApp.openById('1fx59GrC_8WEU5DCM5y2N8YR3PK936RLqZVknQ66WWUA'); //replace with source ID 
 
    var sourceSheet = sheetId.getSheetByName('Notes'); //replace with source Sheet tab name 
 
    var notesActiveCell = sourceSheet.getActiveCell(); 
 
    var notesRow = notesActiveCell.getRow(); 
 
    var notesComp = SpreadsheetApp.getActiveSheet().getRange('B' + notesRow.toString()); 
 

 
    
 
    var destSheet = sheetId.getSheetByName('Companies'); //replace with destination Sheet tab name 
 
    var compActiveCell = destSheet.getActiveCell(); 
 
    var compRow = compActiveCell.getRow(); 
 
    var companies = SpreadsheetApp.getActiveSheet().getRange('B' + compRow.toString()); 
 
    
 
    
 
    if (notesComp.getValues() == companies.getValues()) { 
 
    Logger.log(notesComp.getValues()); 
 
    var sourceRange = SpreadsheetApp.getActiveSheet().getRange('C' + notesRow.toString()); //assign the range you want to copy 
 
    var notesData = sourceRange.getValues(); 
 
    destSheet.getRange('F' + compRow.toString()).setValues(notesData); 
 
    } 
 
}

回答

0

代碼中有很多錯誤。我不是一個非常有經驗的程序員,但形成你所說的話我認爲下面的腳本應該給你一個很好的起點。

不要忘記,您還需要添加onEdit觸發器,以便在輸入值時運行腳本。

function onEdit(e) { 

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sourceSheet = ss.getActiveSheet(); 
var sourceCellRow = sourceSheet.getActiveCell().getRowIndex(); 
var sourceData = sourceSheet.getRange(sourceCellRow, 1, 1, 2).getValues(); 

var destSS = SpreadsheetApp.openById('Paste destination Spreadsheet ID here'); 
var destSheet = destSS.getSheetByName('Companies'); 
var searchData = destSheet.getRange(1, 1,  destSheet.getLastRow()).getValues(); 

for (var i = 0; i < searchData.length; i ++){ 
    if(searchData[i][0] == sourceData[0][0]){ 
     destSheet.getRange(i + 1, 2).setValue(sourceData[0][1]); 
     break; 
    }  
    } 
    } 
0

嘗試使用copyTo(destination)

複製數據從單元格範圍至細胞的另一範圍。值和格式都被複制。

// The code below will copy the first 5 columns over to the 6th column. 
var sheet = SpreadsheetApp.getActiveSheet(); 
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5); 
rangeToCopy.copyTo(sheet.getRange(1, 6)); 
} 

我認爲這將是你onEdit()函數中非常寶貴的。\

您可能還需要檢查從this google forum

function Copy() { 
var sss = SpreadsheetApp.openById('spreadsheet_key'); //replace with source ID 
var ss = sss.getSheetByName('Source'); //replace with source Sheet tab name 
var range = ss.getRange('A2:E6'); //assign the range you want to copy 
var data = range.getValues(); 

var tss = SpreadsheetApp.openById('spreadsheet_key'); //replace with destination ID 
var ts = tss.getSheetByName('SavedData'); //replace with destination Sheet tab name 
ts.getRange(ts.getLastRow()+1, 1,5,5).setValues(data); //you will need to define the size of the copied data see getRange() 
} 
這個樣本
相關問題