1

I want to Set Formula for some cell, depend on how much data I have. The problem was, I can't write script to make Range without Row Initial ( e.g Range(A:AA)). To make just Column Initial, I try to replace Parameter : lastCol and lastRow with blank string ('') , and Zero Number (0), but not work. Thanks

function recap() {

var   sheet             =   SpreadsheetApp.getActiveSpreadsheet()
var   sheetForm         =   sheet.getSheetByName('METER')
const sheetPrint        =   sheet.getSheetByName('CETAK TAGIHAN')
const n                 =   5
var   lastRow           =   sheetForm.getLastRow()
var   lastCol           =   n+4
const startRow          =   7 
const currentCol        =   3 

for (let i = 0 ; i < n; i++){

// FORMULA =vlookup((max(METER!A:A)),METER!A:I,5)

// HOW TO SET ALL COLUMN RANGE :(A:I) WITHOUT ROW NUMBER

sheetPrint.getRange(i+startRow,currentCol).setFormula('vlookup((max(METER!A:A)),METER!'+sheetForm.getRange(1,1,lastRow,lastCol).getA1Notation()+','+(5+i)+')');

}

}

  • Can you provide the sample formulas you want to put to the cells "C7:C11"? I guessed "C7:C11" from your script."C7:C11". – Tanaike Feb 17 '23 at 12:24
  • Sample Formula from "C7 : C11" =vlookup((max(METER!A:A)),METER!A1:I101,5) =vlookup((max(METER!A:A)),METER!A1:I101,6) =vlookup((max(METER!A:A)),METER!A1:I101,7) =vlookup((max(METER!A:A)),METER!A1:I101,8) =vlookup((max(METER!A:A)),METER!A1:I101,9) – PSAB Plalangan Feb 17 '23 at 12:26
  • I want the Formula from "C7 : C11", be like : =vlookup((max(METER!A:A)),METER!A:I,5) =vlookup((max(METER!A:A)),METER!A:I,6) =vlookup((max(METER!A:A)),METER!A:I,7) =vlookup((max(METER!A:A)),METER!A:I,8) =vlookup((max(METER!A:A)),METER!A:I,9) – PSAB Plalangan Feb 17 '23 at 12:30
  • Thank you for replying. About `A:I` of `=vlookup((max(METER!A:A)),METER!A:I,5)`, in this case, you are required to receive the last column from "METER" sheet. Is my understanding correct? – Tanaike Feb 17 '23 at 12:32
  • Yes, If the Row Number added in the Range Initial, for now The formula work. If I use the existing Last Row for Now, Its Just 100 Row (default New Sheet Row). So the Formula will be "METER!A1:I100,5" , in the future when the row more than 100, the formula will did not work. – PSAB Plalangan Feb 17 '23 at 12:39
  • Thank you for replying. From your reply, I proposed a modified script as an answer. Please confirm it. If I misunderstood your question and that was not useful, I apologize. – Tanaike Feb 17 '23 at 12:43

1 Answers1

1

In your situation, how about the following modification?

Modified script:

function recap() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet()
  var sheetForm = sheet.getSheetByName('METER')
  const sheetPrint = sheet.getSheetByName('CETAK TAGIHAN')
  const n = 5

  const lastColumn = sheetForm.getLastColumn();
  const columnIndexToLetter_ = index => (a = Math.floor(index / 26)) >= 0 ? columnIndexToLetter_(a - 1) + String.fromCharCode(65 + (index % 26)) : ""; // ref: https://stackoverflow.com/a/53678158
  const columnLetter = columnIndexToLetter_(lastColumn - 1);
  const formulas = [...Array(n)].map((_, i) => [`=vlookup((max(METER!A:A)),METER!A:${columnLetter},${i + 5})`]);
  sheetPrint.getRange(7, 3, formulas.length).setFormulas(formulas);
}
  • When this script is run, the following formulas are put to the cells "C7:C11".

      =vlookup((max(METER!A:A)),METER!A:I,5)
      =vlookup((max(METER!A:A)),METER!A:I,6)
      =vlookup((max(METER!A:A)),METER!A:I,7)
      =vlookup((max(METER!A:A)),METER!A:I,8)
      =vlookup((max(METER!A:A)),METER!A:I,9)
    
  • If the value of METER!A:I is constant, I think that the following modification might be able to be used.

    function recap() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet()
      var sheetForm = sheet.getSheetByName('METER')
      const sheetPrint = sheet.getSheetByName('CETAK TAGIHAN')
      const n = 5
      const formulas = [...Array(n)].map((_, i) => [`=vlookup((max(METER!A:A)),METER!A:I,${i + 5})`]);
      sheetPrint.getRange(7, 3, formulas.length).setFormulas(formulas);
    }
    
  • As another approach, the following modified script might be able to be used. In this case, the number values are removed from a1Notation.

    function recap() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet()
      var sheetForm = sheet.getSheetByName('METER')
      const sheetPrint = sheet.getSheetByName('CETAK TAGIHAN')
      const n = 5
      const a1Notation = sheetForm.getRange(1, 1, 1, sheetForm.getLastColumn()).getA1Notation().replace(/\d/g, "");
      const formulas = [...Array(n)].map((_, i) => [`=vlookup((max(METER!A:A)),METER!${a1Notation},${i + 5})`]);
      sheetPrint.getRange(7, 3, formulas.length).setFormulas(formulas);
    }
    

Note:

  • I think that when the formulas are put using setFormulas, the process cost can be reduced a little.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Wow. It's done ! Thanks @Tanaike for your kindly, to help me fix this. – PSAB Plalangan Feb 17 '23 at 12:47
  • @PSAB Plalangan Thank you for replying and testing it. I'm glad your issue was resolved. I could correctly understand your question with your cooperation. Thank you, too. – Tanaike Feb 17 '23 at 12:48
  • The value of METER!A:I not constant. So I used your previous script. Thanks – PSAB Plalangan Feb 17 '23 at 12:50
  • @PSAB Plalangan Thank you for the additional information. – Tanaike Feb 17 '23 at 12:52
  • Thanks @Tanaike Sorry, because your previous script was work, I don't read your last script here. It's right, the last script more simple and reduce process. As a new learner, It's helpfull and I can use that script on another case in my project. – PSAB Plalangan Feb 22 '23 at 22:31