Home > Back-end >  Google Apps Script multiple autofill
Google Apps Script multiple autofill

Time:10-16

I need your help to collapse this code. I am a beginner and have failed to use variables to do this. Basically, I started with a macro but I am not satisfied with the result. With 2 variables can the code be reduced?

Thank you in advance for your assistance.

    function autofill() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ss_liste = ss.getSheetByName('Liste');

      ss_liste.getRange('K3').autoFill(ss.getRange('K3:K2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('L3').autoFill(ss.getRange('L3:L2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('M3').autoFill(ss.getRange('M3:M2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('N3').autoFill(ss.getRange('N3:N2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('O3').autoFill(ss.getRange('O3:O2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('P3').autoFill(ss.getRange('P3:P2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('Q3').autoFill(ss.getRange('Q3:Q2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('R3').autoFill(ss.getRange('R3:R2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('S3').autoFill(ss.getRange('S3:S2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
      ss_liste.getRange('T3').autoFill(ss.getRange('T3:T2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
    };

CodePudding user response:

You don't need so many individual getRange() calls:

function autofill() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const ss_liste = ss.getSheetByName('Liste')

  ss_liste.getRange('K3:T3').autoFill(ss.getRange('K3:T2'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES)
}

I hope this is helpful to you!

  • Related