Copy a value from one cell and paste it to another - Google Apps Script

Viewed 1103

I have the following script:

function myFunction()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Eric");
ss.setActiveSheet(sheet).setActiveSelection("D50");
}

I want to copy the value (but not the formula) of cell AG1 to cell D50.

1 Answers

Explanation:

As far as I understand, your goal is to get the value of cell AG1 and set it to cell D50.

  • You don't need to set active ranges.

  • You just need to apply getValue() to the AG1 range to get the value and setValue(value) to D50 to set the value.

Solution:

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Eric");
var AG1val = sheet.getRange('AG1').getValue(); // get the value of AG1
sheet.getRange("D50").setValue(AG1val); // set the value of AG1 to D50  
}
Related