CSV icon indicating copy to clipboard operation
CSV copied to clipboard

Last column not being detected

Open linux478 opened this issue 3 years ago • 1 comments

I am a beginner to Autohotkey and I though this library would help me complete what I wanted to do. Currently, I am just reading in a tsv file (tab separated value file). The file seams to load. When I output the headers, the message box is missing the last column.

Script Source Code

#include csv.ahk
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

csv_filename := "timecard2.txt"

FormatTime, CurrentYear,, yyyy
FormatTime, CurrentMonthDay,, MM-dd
FormatTime, CurrentWeekday,, ddd
FormatTime, CurrentTime,, hh:mm
Week := SubStr(A_YWeek, -1)

CSV_Load(csv_filename,CSV,"`t")

row := CSV_ReadRow(CSV,1)

Msgbox %row%

Data file

Date	Clock-In	Clock-Out
2022-45-11-07-Mon	07:21	04:37
2022-45-11-08-Tue	07:05	04:19
2022-45-11-09-Wed	07:07	04:13
2022-45-11-10-Thu	07:04	04:02
2022-45-11-11-Fri	07:07

Files

time_punch - Copy.txt timecard2.txt

linux478 avatar Nov 11 '22 14:11 linux478

In this case it is due to the fact the last line in the TSV only has two columns, so after 07:07 on row 6 there is no tab character, so the file is assumed to have two columns. If you add a tab character or tab+value it would work:

Date	Clock-In	Clock-Out
12022-45-11-07-Mon	207:21	304:37
2022-45-11-08-Tue	07:05	04:19
2022-45-11-09-Wed	07:07	04:13
2022-45-11-10-Thu	07:04	04:02
2022-45-11-11-Fri	07:07	04:05

So you should either ensure that the source of the data (program with export function I assume) writes all cells and thus columns (even if empty) or perform a check prior to reading the data and "append" a missing cell in the last row. Note that if it had been any other row -- you can test this by removing the tab + cell from row 2 for example -- it wouldn't have caused an issue. Only because it happens to be the last row you now notice it. (I didn't write the library although I did contribute to it at the time - and as I'm using it in production and I assume others too I'm hesitant to post updates to fix such things)

Syntax tip: the "name" of the identifier (you use CSV) should be quoted, so "CSV" unless you use CSV as a variable like you have done with csv_filename

CSV_Load(csv_filename,"CSV","`t")
row := CSV_ReadRow("CSV",1)

hi5 avatar Nov 11 '22 18:11 hi5