Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last column not being detected #4

Open
linux478 opened this issue Nov 11, 2022 · 1 comment
Open

Last column not being detected #4

linux478 opened this issue Nov 11, 2022 · 1 comment

Comments

@linux478
Copy link

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

@hi5
Copy link
Owner

hi5 commented Nov 11, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants