jamietr1 / google-docs-writing-tracker

Automation scripts to track writing in Google Docs
187 stars 39 forks source link

When Word Count Mode was set to 0, reporting word count only ever returns 0 #9

Closed iomadh closed 10 years ago

iomadh commented 10 years ago

When Word Count Mode was set to 0, I was not getting the words reported in the almanac,

You wrote a total of 0 words. These breakdown as follows:
Fiction/Nonfiction
* Current daily writing goal: 100 words
* You wrote a total of words:
* You have now written for 4 out of the last 7 days.
* You have now written for 4 consecutive days.

    * That is a new consecutive-day record!

Looking at the code, specifically the calls to getWordsWritten the logic seemed odd in that it is only ever called with "fiction", "nonfiction" or "blogging" never "total"

I copied the data from column D (Total) to column B(Fiction) and got the correct output

You wrote a total of 134 words. These breakdown as follows:
Fiction/Nonfiction
* Current daily writing goal: 100 words
* You wrote a total of 134 words:
* You have now written for 4 out of the last 7 days.
* You have now written for 4 consecutive days.

    * That is a new consecutive-day record!

* You exceeded your daily writing goal of 100 words by 34 words.
* You've hit your goal for 2 consecutive days.
iomadh commented 10 years ago

I think the problem is line 108

  if (MODE == 1) {
    /* ASSERT: Split into fiction/nonficiton */
    var ficWords = getWordsWritten(almanac_day, "Writing", "fiction");
    var nfWords = getWordsWritten(almanac_day, "Writing", "nonfiction");
    var totalFicNonFicWords = ficWords + nfWords;
    var fictionPercent = (ficWords/totalFicNonFicWords)*100;
    var nonFictionPercent = 100 - fictionPercent;
  } else {
    /* ASSERT: Don't split, just give the totals */
    var ficWords = getWordsWritten(almanac_day, "Writing", "fiction");
    var totalFicNonFicWords = ficWords;
  } 

getWordsWritten is called with "fiction" when MODE is not 1 but the fiction column is empty

jamietr1 commented 10 years ago

I believe I have fixed this. The problem wasn't entirelyin the 108 section above because you'll note that in the else clause, varTotalFicNonFicWord is to ficWords. The problem was in getWordsWritten, which wasn't looking in the proper column for total words when Word Count Mode = 0. In order to fix it, I've changed code in 2 place, first, up by 108:

if (MODE == 1) {
    /* ASSERT: Split into fiction/nonficiton */
    var ficWords = getWordsWritten(almanac_day, "Writing", "fiction");
    var nfWords = getWordsWritten(almanac_day, "Writing", "nonfiction");
    var totalFicNonFicWords = ficWords + nfWords;
    var fictionPercent = (ficWords/totalFicNonFicWords)*100;
    var nonFictionPercent = 100 - fictionPercent;
  } else {
    /* ASSERT: Don't split, just give the totals */
    var ficWords = getWordsWritten(almanac_day, "Writing", "total");
    var totalFicNonFicWords = ficWords;
  }  

Note that in the else clause, it now passes almanac_day, "writing" and "total". That latter will kick in below:

function getWordsWritten(date, type, wordType) {
  var qs_doc = SpreadsheetApp.openById(WRITING_DATA);
  var sheet = qs_doc.getSheetByName(type);
  var words = 0;

  var range = sheet.getRange("A2:A" + sheet.getLastRow());
  var match = findDate(date, range);
  Logger.log(match);

  if (wordType == "fiction") {
    /* ASSERT: capture fiction writing */
    try {
      words = sheet.getRange(COL_FICTION + match.getRow()).getValue();
    } catch (e) {
      words = 0;
    }
  } else if (wordType == "nonfiction") {
    /* ASSERT: capture nonfiction writing */
    try {
      words = sheet.getRange(COL_NONFICTION + match.getRow()).getValue();
    } catch (e) {
      words = 0;
    }
  } else if (wordType == "Blogging") {
    /* ASSERT: capture blog writing */
    try {
      words = sheet.getRange(COL_BLOGGING_TOTAL + match.getRow()).getValue();
    } catch (e) {
      words = 0; 
    }    
  } else if (wordType == "total") {
    /* ASSERT: capture total writing when Word Count Mode = 0 */
    try {
      words = sheet.getRange(COL_WRITING_TOTAL + match.getRow()).getValue();
    } catch (e) {
      words = 0; 
    }        
  }
  return words;
}

total will get trapped in the last else clause. You can see that this is using appropriate setting variables for which column to pull the word counts from, and all together reads a little more cleanly.

I tested it with both Word Count Mode = 1 and Word Count Mode = 0 and it worked correctly in both cases. But I'd ask that you update the code and then test it yourself to see if you are getting the same results I am now.

iomadh commented 10 years ago

Tested and confirmed fixed - great stuff - looking forward to this running properly overnight