Search code examples
google-apps-scriptgoogle-sheetscopyselected

Copy selected range in a sheet to other place in a same sheet with Google Scripteditor


In Google Spreadsheets i try to copy some selected cells to another place in the sheet with Google Scripteditor. I have this code, but it doesn't work:

function kopieerDag() {
  var sheet = SpreadsheetApp.getActiveSheet();
  Logger.log(sheet);
  var selection = sheet.getSelection();
  // var values = selection.getValues();
  Logger.log(selection);
  selection.copyTo(sheet.getRange(1, 1), {contentsOnly: true});
}

any ideas? thx for the help!


Solution

  • The reason of error in your script is that getSelection() is not directly returned the range object. Please use getActiveRange() for getSelection(). So can you try this modification?

    From :

    var selection = sheet.getSelection();
    

    To :

    var selection = sheet.getSelection().getActiveRange();
    

    References :