Home > Enterprise >  Script to copy values only from multiple tabs to another spreadsheet
Script to copy values only from multiple tabs to another spreadsheet

Time:06-21

I have found so many scripts to copy values only from one spreadhseet to another. However, all of them is to copy the whole spreasheet.

I am very new with google script and cannot find a way to copy values only from specific tabs to another spreasheet adding these new tabs to it.

function temp() {

  var sss = SpreadsheetApp.openById('XYZ'); // sss = source spreadsheet
  //var ss = sss.getSheets()[4]; // ss = source sheet

  var ss = sss.getSheets(); // ss = source sheet
  var id=4; //default number

  for(var i in ss)
  {
    var sheet = ss[i];
    if(sheet.getName()== "ABC")
    {  id=i;
      break;
    }
  }
  console.log(id);
  
  ss=sss.getSheets()[id];

  //Get full range of data
  var SRange = ss.getDataRange();
  //get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();
  //get the data values in range
  var SData = SRange.getValues();
  SpreadsheetApp.flush();
  var tss = SpreadsheetApp.getActiveSpreadsheet(); // tss = target spreadsheet
  var ts = tss.getSheetByName('ABC'); // ts = target sheet
  //set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);


}

Thanks a lot in advance.

CodePudding user response:

Issue:

  • You can to copy the values from a specific set of sheets to a new spreadsheet.
  • I assume that you don't want to copy the sheet itself, but only the values from the source sheet.

Modifications:

  • Use filter to get an array of your desired sheets (based on sheet name).
  • If the target spreadsheet doesn't have a sheet with that name, create it with Spreadsheet.insertSheet.

Code sample:

function temp() {
  var sheetNames = ["ABC", "DEF"]; // Change accordingly
  var sss = SpreadsheetApp.openById('XYZ');
  var sheetsToCopy = sss.getSheets().filter(s => sheetNames.includes(s.getSheetName()));
  sheetsToCopy.forEach(ss => {
    var sourceSheetName = ss.getSheetName();
    var SRange = ss.getDataRange();
    var A1Range = SRange.getA1Notation();
    var SData = SRange.getValues();
    var tss = SpreadsheetApp.getActiveSpreadsheet();
    var ts = tss.getSheetByName(sourceSheetName);
    if (!ts) ts = tss.insertSheet(sourceSheetName);
    ts.getRange(A1Range).setValues(SData);    
  });
}

CodePudding user response:

Copy these sheets to another spreadsheet

function copythese() {
  const ss = SpreadsheetApp.getActive();
  const these = ['Sheet0','Sheet1'];//Put your sheetnames here
  const another = SpreadsheetApp.openById("another id");//put the other spreadsheet id here
  ss.getSheets().filter(sh => ~these.indexOf(sh.getName())).forEach(sh => {
    sh.copyTo(another);
  });
}

Append These Values to another spreadsheet

function appendthesevalues() {
  const ss = SpreadsheetApp.getActive();
  const these = ['Sheet0','Sheet1'];
  const another = SpreadsheetApp.openById("another id");
  ss.getSheets().filter(sh => ~these.indexOf(sh.getName())).forEach(sh => {
    let vs = sh.getDataRange().getValues();
    let nsh = another.getSheetByName(sh.getName());
    if(!nsh) {
      nsh = another.insertSheet(sh.getName());
      SpreadsheetApp.flush();
    }
    if(nsh) {
      nsh.getRange(nsh.getLastRow()   1, 1 ,vs.length, vs[0].length).setValues(vs);
    }
  });
}
  • Related