Home > Blockchain >  copyTo(target) copies my functions but need it to copy values
copyTo(target) copies my functions but need it to copy values

Time:09-14

So this might be a simple one for someone more skilled. The code works except it copies the functions rather than the values. The cells contain a LOT of functions and I'm using this to create a second version since the functions are UI data reliant and this is the easiest way to make a "Backup" in case we need to go back to an old forms data. I'm using the below code and it copies everything over, and creates the sheet with the right name, but I'm not sure how to get it to take the values and not the functions.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("AHA");
var nameSheet = sourceSheet.getRange(1, 15).getValue();
var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
var targetSheet = sourceSheet.copyTo(target);
targetSheet.setName(nameSheet);

I've tried this,

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName("AHA");
  var nameSheet = sourceSheet.getRange(1, 15).getValue();
  var target = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
  var targetSheet = sourceSheet.copyTo(target,SpreadsheetApp.CopyPasteType.PASTE_VALUES);
  targetSheet.setName(nameSheet);

but it gives me the following error:

Exception: The parameters (SpreadsheetApp.Spreadsheet,SpreadsheetApp.CopyPasteType) don't match the method signature for SpreadsheetApp.Sheet.copyTo.

Any help or advice on another script I could use would be appreciated. Thanks.

CodePudding user response:

Try this:

function copyvalues() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("AHA");
  const vs = sh.getDataRange().getValues();
  const name = sh.getRange(1, 15).getValue();
  const tss = SpreadsheetApp.openById('1634dptmmyJVtbYLeHKw57U_Wzi2k1JGnACndxFzddgM');
  const tsh = tss.insertSheet(name);
  tsh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}
  • Related