Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oshliaer/ac66acaf7315870d6c578152229d12a2 to your computer and use it in GitHub Desktop.
Save oshliaer/ac66acaf7315870d6c578152229d12a2 to your computer and use it in GitHub Desktop.

Takes a row from another Sheet by the value of the index (when onEdit fires)

Unsplash

You can try this. Enter in the Spreadsheet in the range A:A a value from 1 to 16.

var DATASHEETID = '1gnNuHGReX5Bwc4CbgNcT8mol7Q97TSm9LqkGqhXnazE';
function onEdit(e) {
try{
if(!e || ! e.range) return;
if(e.range.columnStart === 1){
var data = getData();
var entry = getEntryByIndex(data, 0, e.value);
Logger.log(entry);
if(entry.length)
SpreadsheetApp.getActiveSheet().getRange(e.range.rowStart, 2, 1, entry.length).setValues([entry]);
}
}catch(err){
SpreadsheetApp.getActiveSpreadsheet().toast(err.message, 'Error!!1');
}
}
function getData(){
return SpreadsheetApp.openById(DATASHEETID).getRange('Sheet1!A:H').getValues();
}
function getEntryByIndex(data, col, index){
for(var i = 0; i < data.length; i++){
if(data[i][col] == index)
return data[i];
}
return [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment