<commit_before>createSupportSheets = (spreadsheet) ->
  for i in ignoreSheets
    sheetName = ignoreSheets
    sheet = spreadsheet.getSheetByName(sheetName)
    spreadsheet.insertSheet sheetName  if sheet is null
  return

readConfig = ->
  rawConfig = spreadsheet.getSheetByName("Config").getRange("A1:B").getValues()
  for item in rawConfig
    k = item[0]
    v = item[1]
    if k isnt "" and v isnt ""
      config[k] = v
      properties.setProperty "config." + k, v
  config["init"] = true
  verbose = config["verbose"]
  trace = config["trace"]
  Logger.log "readConfig %s", config
  flushLog()
  return

isIgnoreSheet = (name) ->
  name in ignoreSheets

registerSpreadsheetTrigger = (namespace) ->
  if spreadsheet isnt null
    triggers = ScriptApp.getProjectTriggers()
    for i of triggers
      ScriptApp.deleteTrigger triggers[i]
    prefix = ""
    prefix = "#{namespace}." if namespace
    properties.setProperty 'namespace', namespace
    ScriptApp.newTrigger(prefix + "edit").forSpreadsheet(spreadsheet)
    .onEdit().create()
    ScriptApp.newTrigger(prefix + "change").forSpreadsheet(spreadsheet)
    .onChange().create()
    ScriptApp.newTrigger(prefix + "initialize").forSpreadsheet(spreadsheet)
    .onOpen().create()
    dump "triggers %s installed for %s", spreadsheet.getName(),
      ScriptApp.getProjectTriggers().map (item) ->
        item.getHandlerFunction()
    cacheSheetRows()
  else
    dump "spreadsheet was null, no triggers installed"
  flushLog()
  return

flushLog = ->
  if verbose
    logSheet.appendRow [Logger.getLog()]
    Logger.clear()
  return

logState = ->
  dump "logging state."
  sheets = spreadsheet.getSheets()
  for sheet in sheets
    rows = sheet.getMaxRows()
    name = sheet.getName()
    sid = sheet.getSheetId()
    if isIgnoreSheet name
      dump "ignore sheet %s:'%s' rows %s.", sid, name, rows
      continue
    crows = properties.getProperty name
    dump "sheet %s:'%s' rows %s/%s.", sid, name, crows, rows
  dump "triggers %s installed for %s", spreadsheet.getName(),
    ScriptApp.getProjectTriggers().map (item) ->
      item.getHandlerFunction()
  dump config
  dump properties.getProperty 'namespace'
  flushLog()
  return

dump = ->
  Logger.log.apply Logger, arguments if verbose
  return

tdump = ->
  Logger.log.apply Logger, arguments if trace
  return

insertRowEvent = (range) ->
  setupNewRow range
  return

setupNewRow = (range) ->
  dest = range
  row = range.getRow()
  oldValue = dest.getValue()
  if oldValue is ""
    destFormat = spreadsheet.getRange "A#{row}:A"
    sourceFormat = dest.offset -1, 0
    sourceFormat.copyTo destFormat, formatOnly: true
    dest.setValue new Date()
    dump "setupNewRow row %s - %s", row, dest.getA1Notation()
  else
    dump "FAILED - value already set %s - setupNewRow row %s - %s", oldValue,
      row, dest.getA1Notation()
  flushLog()
  return

expireCache = ->
  sheets = spreadsheet.getSheets()
  for sheet in sheets
    name = sheets.getName()
    continue if isIgnoreSheet name
    properties.deleteProperty name
  dump "cleard cache."
  flushLog()
  return

cacheSheetRows = ->
  sheets = spreadsheet.getSheets()
  for sheet in sheets
    rows = sheet.getMaxRows()
    name = sheet.getName()
    sid = sheet.getSheetId()
    continue if isIgnoreSheet name
    properties.setProperty name, rows
    dump "sheet %s:'%s' rows %s.", sid, name, rows
  flushLog()
  return

initialize = (e) ->
  dump "initialize."
  readConfig()
  cacheSheetRows()
  initializeMenus e
  return

initializeMenus = (e) ->
  namespace = properties.getProperty 'namespace'
  ui = SpreadsheetApp.getUi()
  ui.createAddonMenu()
  .addItem 'Log State', "#{namespace}.logState"
  .addToUi()


###
Event handlers
###
onInstall = (namespace) ->
  dump "onInstall"
  readConfig()
  registerSpreadsheetTrigger namespace
  return

###
Called onOpen
###
onOpen = (e) ->
  dump "onOpen."
  initialize(e)
  return

edit = (e) ->
  sheet = e.source.getActiveSheet()
  sheetName = sheet.getSheetName()
  cachedRows = properties.getProperty sheetName
  rows = sheet.getMaxRows()
  dump "onEdit range %s, name %s, event %s, rows %s, cached %s",
    e.range.getA1Notation(), sheetName, e, rows, cachedRows

  unless isIgnoreSheet sheetName
    column = e.range.getColumn()<commit_msg>new menu item to truncate log<commit_after>createSupportSheets = (spreadsheet) ->
  for i in ignoreSheets
    sheetName = ignoreSheets
    sheet = spreadsheet.getSheetByName(sheetName)
    spreadsheet.insertSheet sheetName  if sheet is null
  return

readConfig = ->
  rawConfig = spreadsheet.getSheetByName("Config").getRange("A1:B").getValues()
  for item in rawConfig
    k = item[0]
    v = item[1]
    if k isnt "" and v isnt ""
      config[k] = v
      properties.setProperty "config." + k, v
  config["init"] = true
  verbose = config["verbose"]
  trace = config["trace"]
  Logger.log "readConfig %s", config
  flushLog()
  return

isIgnoreSheet = (name) ->
  name in ignoreSheets

registerSpreadsheetTrigger = (namespace) ->
  if spreadsheet isnt null
    triggers = ScriptApp.getProjectTriggers()
    for i of triggers
      ScriptApp.deleteTrigger triggers[i]
    prefix = ""
    prefix = "#{namespace}." if namespace
    properties.setProperty 'namespace', namespace
    ScriptApp.newTrigger(prefix + "edit").forSpreadsheet(spreadsheet)
    .onEdit().create()
    ScriptApp.newTrigger(prefix + "change").forSpreadsheet(spreadsheet)
    .onChange().create()
    ScriptApp.newTrigger(prefix + "initialize").forSpreadsheet(spreadsheet)
    .onOpen().create()
    dump "triggers %s installed for %s", spreadsheet.getName(),
      ScriptApp.getProjectTriggers().map (item) ->
        item.getHandlerFunction()
    cacheSheetRows()
  else
    dump "spreadsheet was null, no triggers installed"
  flushLog()
  return

flushLog = ->
  if verbose
    logSheet.appendRow [Logger.getLog()]
    Logger.clear()
  return

logState = ->
  dump "logging state."
  sheets = spreadsheet.getSheets()
  for sheet in sheets
    rows = sheet.getMaxRows()
    name = sheet.getName()
    sid = sheet.getSheetId()
    if isIgnoreSheet name
      dump "ignore sheet %s:'%s' rows %s.", sid, name, rows
      continue
    crows = properties.getProperty name
    dump "sheet %s:'%s' rows %s/%s.", sid, name, crows, rows
  dump "triggers %s installed for %s", spreadsheet.getName(),
    ScriptApp.getProjectTriggers().map (item) ->
      item.getHandlerFunction()
  dump config
  dump properties.getProperty 'namespace'
  flushLog()
  return

truncateLog = ->
  rows = logSheet.getMaxRows()
  logSheet.deleteRows 1, rows - 1

dump = ->
  Logger.log.apply Logger, arguments if verbose
  return

tdump = ->
  Logger.log.apply Logger, arguments if trace
  return

insertRowEvent = (range) ->
  setupNewRow range
  return

setupNewRow = (range) ->
  dest = range
  row = range.getRow()
  oldValue = dest.getValue()
  if oldValue is ""
    destFormat = spreadsheet.getRange "A#{row}:A"
    sourceFormat = dest.offset -1, 0
    sourceFormat.copyTo destFormat, formatOnly: true
    dest.setValue new Date()
    dump "setupNewRow row %s - %s", row, dest.getA1Notation()
  else
    dump "FAILED - value already set %s - setupNewRow row %s - %s", oldValue,
      row, dest.getA1Notation()
  flushLog()
  return

expireCache = ->
  sheets = spreadsheet.getSheets()
  for sheet in sheets
    name = sheets.getName()
    continue if isIgnoreSheet name
    properties.deleteProperty name
  dump "cleard cache."
  flushLog()
  return

cacheSheetRows = ->
  sheets = spreadsheet.getSheets()
  for sheet in sheets
    rows = sheet.getMaxRows()
    name = sheet.getName()
    sid = sheet.getSheetId()
    continue if isIgnoreSheet name
    properties.setProperty name, rows
    dump "sheet %s:'%s' rows %s.", sid, name, rows
  flushLog()
  return

initialize = (e) ->
  dump "initialize."
  readConfig()
  cacheSheetRows()
  initializeMenus e
  return

initializeMenus = (e) ->
  namespace = properties.getProperty 'namespace'
  ui = SpreadsheetApp.getUi()
  menu = ui.createAddonMenu()
  menu.addItem 'Log State', "#{namespace}.logState"
  .addToUi()
  menu.addItem 'Truncate Log', "#{namespace}.truncateLog"
  .addToUi()


###
Event handlers
###
onInstall = (namespace) ->
  dump "onInstall"
  readConfig()
  registerSpreadsheetTrigger namespace
  return

###
Called onOpen
###
onOpen = (e) ->
  dump "onOpen."
  initialize(e)
  return

edit = (e) ->
  sheet = e.source.getActiveSheet()
  sheetName = sheet.getSheetName()
  cachedRows = properties.getProperty sheetName
  rows = sheet.getMaxRows()
  dump "onEdit range %s, name %s, event %s, rows %s, cached %s",
    e.range.getA1Notation(), sheetName, e, rows, cachedRows

  unless isIgnoreSheet sheetName
    column = e.range.getColumn()
{'fraction_non_alphanumeric': 0.11960132890365449, 'fraction_numerical': 0.0020620918776492153, 'mean_word_length': 3.4677584442169906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '567330', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2748, 'n_words': 745}