Home > front end >  How to setFormulas() to a range using Google Apps Script?
How to setFormulas() to a range using Google Apps Script?

Time:12-20

What do I need to change so that this script sets the formulas (from row 2) to the range defined? The code below doesn't give me any error, but it doesn't set the formulas at all.

  var lc = sheet.getLastColumn();
  var range = sheet.getRange(2, 1, 1, lc);
  var formulas = range.getFormulas();
  
  range.copyFormatToRange(sheet, 1, lc, 3, 3 response); //This pastes the format to the range, whose number of rows is defined depending on the # that the user inputs.

  var pasteTo = sheet.getRange(3, 1, response, lc);//Defines the range for setting formulas
  for (var a = 3; a < response; a  ){
    pasteTo = sheet.getRange("A"   a,sheet.getLastColumn());
    pasteTo.setFormulas(formulas);
  }
  }
}

CodePudding user response:

Doing everything in Arrays is much faster

function kdkdkd() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const osh = ss.getSheetByName('Sheet1');
  const fA = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getFormulas();
  osh.getRange(row,col,fA.length,fA[0].length).setFormulas(fA);  
}

pick whatever you want for row and col

CodePudding user response:

Thank you, @TheMaster, for the orientation. Here's how I solved it:

  var lc = sheet.getLastColumn();
  var range = sheet.getRange(2, 1, 1, lc);
  var formulas = range.getFormulas();
  
  range.copyFormatToRange(sheet, 1, lc, 3, 3 response);

  var pasteTo = sheet.getRange(3, 1, response, lc);
  for (var a = 1; a <= response; a  ){
    pasteTo = sheet.getRange(a 2,1,1,lc);
    pasteTo.setFormulas(formulas);
    }
  }
}

Thank you!

  • Related