2

I'm trying to understand how I can remove a decimal. I have tried Math.trunc() but it never seems to remove. Logger.log(esValues.length) prints 500.0, I tried adding var removeDecimal = Math.trunc(esValues.length) then printing removeDecimal but I still get 500.0

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var es = ss.getSheetByName("School1");
  var ms = ss.getSheetByName("School2");
  var hs = ss.getSheetByName("School3");
  var esValues = es.getDataRange().getValues();
  var counterDell = 0;
  var counterHP = 0;
  //var esValues = es.getRange('A:C').getValues();
  Logger.log(esValues.length); //This prints 500.0, Also tried adding here Logger.log(Math.trunc(esValues.length))
  for (var i = 0; i < esValues.length; i++) {
    var data = esValues[i];
    var first = data[1];
    var last = data[0];
    var middle = data[2];
    var studentid = data[3];
    var device = data[4];
    if (device.includes("Dell")) {
      counterDell++;
    } else if (device.includes("HP")) {
      counterHP++;
    }

  }
  Logger.log(`Dell count is ${counterDell}`)
  Logger.log(`Hp count is ${counterHP}`)
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    [`Math.trunc` returns a number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc). What you want is a string. Use `toFixed(0)`. – Heretic Monkey Aug 10 '22 at 18:25
  • Does this answer your question? [How to format numbers as currency strings](https://stackoverflow.com/questions/149055/how-to-format-numbers-as-currency-strings) – Heretic Monkey Aug 10 '22 at 18:26

2 Answers2

2
Logger.log(esValues.length.split('.')[0])

breaks the decimal into integer and decimal portions, then we discard the decimal portion.

Matt Pengelly
  • 1,480
  • 2
  • 20
  • 34
  • I think @Cooper's answer is correct, .length wouldnt be decimal anyway and the Logger API must be formatting it within. Which i believe makes this answer incorrect for OPs purposes – Matt Pengelly Aug 11 '22 at 14:06
2

Actually just not using the logger will elimate the .0. For some stupid reason it has always done that.

function lfunko() {
  var ss = SpreadsheetApp.getActive();
  var es = ss.getSheetByName("School1");
  var vs = es.getDataRange().getValues();
  var counterDell = 0;
  var counterHP =
  for (var i = 0; i < vs.length; i++) {
    var device = vs[4];
    if (device.includes("Dell")) {
      counterDell++;
    } else if (device.includes("HP")) {
      counterHP++;
    }
  }
  const msg = `HP: ${counterHP} DELL: ${counterDell}`
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(msg),"Counts");
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Do I need to create a function that adds a menu also for this to work? Using it alone doesnt seem to give me a dialog – Mr. Anderson Aug 10 '22 at 22:22
  • try it again... – Cooper Aug 10 '22 at 22:29
  • hmm, did something change here? Looks the same. I dont get a dialog on the sheet ```const msg = `HP: ${counterHP} DELL: ${counterDell}` SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(msg),"Counts");``` – Mr. Anderson Aug 10 '22 at 22:53
  • I just tested it and it works for me. I changed this line: `var device = vs[4];` – Cooper Aug 10 '22 at 22:58
  • Sorry, maybe Im not understanding the full process. If I run this within apps script I get error Exception: Cannot call SpreadsheetApp.getUi() from this context. When I goto the spreadsheet and refresh or open nothing seems to happen? Am I missing something obvious? – Mr. Anderson Aug 11 '22 at 12:23
  • I ran the code from within apps script and it ran just fine. I don't understand why opening the spreadsheet or refreshing should do anything with regard to the script. Are you trying to run it from a trigger? – Cooper Aug 11 '22 at 13:25
  • i think this is the answer, i was wondering why the .0 would show up at all in a length prop. the Logger API mustve been what was formatting the number that way, im sure there are config params in the Logger API. – Matt Pengelly Aug 11 '22 at 14:04
  • @MattPengelly yeah maybe I never really let it bother me too much. I just moved on to debugging my code and move on. – Cooper Aug 11 '22 at 14:12
  • @Cooper No trigger trying to be used, wasn't sure if I needed one? I get same error: https://stackoverflow.com/questions/70895185/exception-cannot-call-spreadsheetapp-getui-from-this-context – Mr. Anderson Aug 11 '22 at 15:38
  • Well I cannot reproduce the error – Cooper Aug 11 '22 at 17:51