google-apps-script Copy a value from one sheet to current sheet


Example

Imagine that we have a separate Google spreadsheet, and we need to get the B2 cell value to cell D5 on your current sheet.

function copyValueandPaste()
{  
    var source = SpreadsheetApp.openById('spread sheet id is here'); //Separate spreadsheet book
    var sourcesheet = source.getSheetByName('Sheet1'); //Sheet tab with source data
    var sourceCellValue = sourcesheet.getRange('B2').getValue(); // get B2 cell value
    
    var thisBook = SpreadsheetApp.getActive(); // Active spreadsheet book
    var thisSheet = thisBook.getSheetByName('Sheet1'); // Target sheet
    thisSheet.getRange('D5').setValue(sourceCellValue); //Set value to target sheet D5 cell
}

You can find spreadsheet id from your URL.