Home > Net >  Script to copy and paste range from current sheet
Script to copy and paste range from current sheet

Time:05-06

I have a script that I have been using to copy and past a range of cells on a specific sheet. It works great, but how can I make the same script work on multiple sheets?

function dcopy1() {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

// Copy from 177th row, 9th column 
var valuesToCopy = sheetFrom.getRange(177, 9, 17, 1).getValues();

//Paste to 5th row, 3rd column
sheetTo.getRange(5,3,valuesToCopy.length,1).setValues(valuesToCopy);
}

CodePudding user response:

Copy multiple sheets

function dcopy1() {
  const ss = SpreadsheetApp.getActive();
  const shto = ss.getSheetByName("MasterSheet");
  const incl = ["Sheet1", "Sheet2", "Sheet3"];//sheets to include by name
  ss.getSheets().filter(s => incl.includes(s.getName())).forEach(sh => {
    let vs = sh.getRange(177, 9, 17, 1).getValues();
    if (shto.getLastRow() < 5) {
      shto.getRange(5, 3, vs.length, vs[0].length).setValues(vs);
    } else {
      shto.getRange(shto.getLastRow()   1, 3, vs.length, vs[0].length).setValues(vs);
    }
  })
}

CodePudding user response:

Hey since u want to copy the values from the same sheet from source location to destination location:

function dcopy1() {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

// Copy from 177th row, 9th column 
var valuesToCopy = sheetFrom.getRange(177, 9, 17, 1).getValues();

//Paste to 5th row, 3rd column
sheetTo.getRange(5,3,valuesToCopy.length,1).setValues(valuesToCopy);
}
  • Related