1

I am running an repeated import of data including a duration value from csv. The duration value is after import read by Google Script and processed. Afterwards the sheet is cleared and expecting new import. The duration value comes sometimes in as time object and is processed correctly. More often the script crashes because instead of timeobject or string duration is a number, like: 0.04758265 ... and when asked for .getHours() there is no.

Question 1: How do I programatically format the column to duration format after clear?

Question 2: How to convert the number to something processable by .getHours() method?

EDIT: Code as requested.

//import snippet
  var file = DriveApp.getFilesByName(fileName);
  mysheet.getRange(startRow,1,1000,30).clearContent();

  var csvFile = file.getBlob().getDataAsString();
  var csvData = CSVToArray(csvFile,"/");

  var rowi = 0;
  for ( var i=3, lenCsv=csvData.length; i<lenCsv; i++ ) {
    if (csvData[i][0].length > 0) {
      if (csvData[i][8].indexOf("*")==-1) {
        csvData[i][8] = "";
      }
      mysheet.getRange((rowi+startRow), startCol, 1, csvData[i].length).setValues(new Array(csvData[i]));
      rowi++;
    }
  }

//snippet of function where i get error
var durArr = mysheet.getRange("L4:L997").getValues();
var noHours;
for (var i=0;i<durArr.length;i++) {

if (durArr[i][0] != '') {
  var valType = typeof durArr[i][0];
    //the bellow line breaks the code
    if (durArr[i][0].getHours() == 0) {noHours+=1}

if (noHours<100) {
  return true;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Stefan
  • 828
  • 12
  • 24
  • What does the input data look like? What does your script do? – Robin Gertenbach Dec 21 '16 at 12:56
  • It's just what I written above. The input data is read in from CSV. In the CSV the data is a usual duration string for example: 0:40:00. But when touched by Google Script it's sometimes used as string, sometimes as number sometimes as duration object. The script just asks for .getHours() and counts if there is or isn't hours. Nothing else. – Stefan Dec 21 '16 at 13:00
  • Sorry, I meant literally what the script does, not conceptually. I.e. I'd like to see the code. – Robin Gertenbach Dec 21 '16 at 13:02
  • Ok. good point to always do that. When preparing a shotend version I noticed the read-in is not asking the right sheet ... wonder how that worked all the time ... o_O Might not be the issue. Code snippets included. – Stefan Dec 21 '16 at 13:21
  • Are you using [this parser](http://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data)? Did you try the [builtin parser](https://developers.google.com/apps-script/reference/utilities/utilities#parseCsv(String,Char))? – Robin Gertenbach Dec 21 '16 at 20:27

0 Answers0