AutoHotkey Community

It is currently May 27th, 2012, 3:57 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: January 20th, 2006, 3:54 am 
Offline

Joined: November 26th, 2005, 2:58 am
Posts: 6
Location: Ottawa, Canada
Hello all,

While working on one of my first scripts, I needed a way to edit a text file via a listview. I found garry's post in this thread: but there were obvious limitations. I've made modifications to his code that allows for deletion and overwriting of a record in a CSV file via a List View.
I'm proud of this since it's the first big problem I solved on my own (i.e. without finding a post or help file example that could get me out of my jam.)

I'm pretty new to this and I'd love to what you all think (i.e. could this have been done a better way??)

Code:
Comment from moderator: See improved versions further below.


edit: removed "gMyListView" from Gui,Show command in code.

_________________
It's all about balance


Last edited by 1uvaknd on January 21st, 2006, 3:51 am, edited 4 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2006, 9:26 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
As you wish:

Here are my comments:

a) I do not know what's the purpose of having "gMyListView" in the Gui,Show command. Could you clarify?

b) If you have a large CSV file your code will create a lot of disc access, since each time you fill the listview, edit, delete or add a address the file needs to be read or written. This is generally slow. I would recommend to read the file into memory (e.g. as a whole into a var with fileread, or into an array with Loop,Read). Then you can do all the operations in memory and write the file out once when done.

c) Gui 2 and Gui 3 are nearly the same, you could simplify the code with combining the GUIs and their code.

d) the if statements could be simplified to
Code:
D := False
if (A41<>A31 or A42<>A32 or A43<>A33  or A44<>A34)
  D := True


Happy scripting

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2006, 10:29 am 
Would you (or a moderator) mind to slightly change the subject line. The "How to" indicates that its a request for Help (which would be at the wrong Forum Section). A unique "ProductName" for your script/app would be of help. Thx for listening.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2006, 3:44 am 
Offline

Joined: November 26th, 2005, 2:58 am
Posts: 6
Location: Ottawa, Canada
Thanks for the tips toralf...I'll work on those and re-post soon. The gMyListView in the Show command was a mistake...I will delete it from the original post.

As for a unique title...like I said in my first post, it's not really my script. I just modified to add some functionality.

I changed the title of my post, as requested.


to be continued...

_________________
It's all about balance


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2006, 5:04 am 
Offline

Joined: November 26th, 2005, 2:58 am
Posts: 6
Location: Ottawa, Canada
Here is the updated code based on toralf's suggestions. Better?

Code:
#NoTrayIcon
#SingleInstance
F1=ADRESS.CSV
IfNotExist, %F1%
{
  text=col1,col2,col3,col4
  text2=name,email,url,program
  FileAppend,%text%`n, %F1%
   FileAppend,%text2%`n, %F1%

}

Gui, Color, 000000
Gui, Font,  S10 CDefault , FixedSys
Gui,Add, ListView,grid r20 w780 +hscroll altsubmit vMyListView gMyListView, Name|EMAIL|URL|PROGRAM
Gui, Add, Button, x10 y356 w100 h20,AddName
Gui, Add, Button, gDELETE x110 y356 w100 h20,DeleteName
Gui,Font,S8 cwhite, Verdana
Gui,Add,Text, x400 y356, %A_AHKVERSION%
Gui, Show, x2 y0 w800 h380 ,LISTVIEW
GoSub,FILLLIST
return

FILLLIST:
LV_Delete()
Loop, read, %F1%
{   
   StringSplit, Array, A_LoopReadLine, `,
   LV_Add("",Array1,Array2,Array3,Array4)
   
}
LV_ModifyCol(1,"AutoHdr")
LV_ModifyCol(2,"AutoHdr")
LV_ModifyCol(3,"AutoHdr")
LV_ModifyCol(4,"AutoHdr")
return

MyListView:
;GuiControlGet, MyListView
if A_GuiEvent = DoubleClick
{
RowNumber = 0  ; This causes the first loop iteration to start the search at the top of the list.
Loop
{
   RowNumber := LV_GetNext(RowNumber)  ; Resume the search at the row after that found by the previous iteration.
   if not RowNumber  ; The above returned zero, so there are no more selected rows.
      break
   LV_GetText(A31, RowNumber,1)
   LV_GetText(A32, RowNumber,2)
   LV_GetText(A33, RowNumber,3)
   LV_GetText(A34, RowNumber,4)
   Line=%A31%,%A32%,%A33%,%A34%
   Break
}
editadd=edit
GoSub,SMGUI
}
return

SMGUI:
Gui,2:Font,  S10 CDefault , FixedSys
Gui,2:Add,Text, x1  y5 w80  h20, NAME
Gui,2:Add,Edit, x60 y5 w140 h20 vA41, %A31%
Gui,2:Add,Text, x1  y35 w80  h20, EMAIL
Gui,2:Add,Edit, x60 y35 w420 h20 vA42, %A32%
Gui,2:Add,Text, x1  y65 w80  h20, WWW
Gui,2:Add,Edit, x60 y65 w420 h20 vA43, %A33%
Gui,2:Add,Text, x1  y95 w80  h20, PROGRAM
Gui,2:Add,Edit, x60 y95 w420 h20 vA44, %A34%
Gui,2:Add, Button, x530 y120 w40 h25, OK
Gui,2:Show, x290 y385 w580 h150, NAME-INPUT
return
 
2ButtonOK:
2GuiClose:
2GuiEscape:
Gui,2:submit
if editadd=edit
{
D := False
if (A41<>A31 or A42<>A32 or A43<>A33  or A44<>A34)
D := True
IF D>0
{
FileAppend, %A41%`,%A42%`,%A43%`,%A44%`n, %F1%
}
current=   
Loop, read, %F1%
{
   IfInString, A_LoopReadLine, %Line%
      {
      Continue
      }
      else
      {
      current=%current%%A_LoopReadLine%`n
      }
}
FileDelete,%F1%
FileAppend,%current%,%F1%

Gui,1:Default
GoSub,FILLLIST
}
if editadd=add
{
FILEAPPEND,%A41%`,%A42%`,%A43%`,%A44%`n, %F1%
Gui,1:Default
GoSub,FILLLIST
}
Gui,2:Destroy
D:=False
return

DELETE:
currentdel=
Msgbox,4,, Are you sure?
Ifmsgbox, YES
{
RowNumber = 0  ; This causes the first loop iteration to start the search at the top of the list.
Loop
{
   RowNumber := LV_GetNext(RowNumber)  ; Resume the search at the row after that found by the previous iteration.
   if not RowNumber  ; The above returned zero, so there are no more selected rows.
      break
   LV_GetText(A31, RowNumber,1)
   LV_GetText(A32, RowNumber,2)
   LV_GetText(A33, RowNumber,3)
   LV_GetText(A34, RowNumber,4)
   Line=%A31%,%A32%,%A33%,%A34%
   Break
}
Loop, read, %F1%
{
   IfInString, A_LoopReadLine, %Line%
      {
      Continue
      }
      else
      {
      currentdel=%currentdel%%A_LoopReadLine%`n
      }
}
FileDelete, %F1%
FileAppend,%currentdel%,%F1%
GoSub,FILLLIST
}
return

ButtonAddName:
A31=
A32=
A33=
A34=
editadd=add
GoSub,SMGUI
return

GuiClose:
ExitApp

_________________
It's all about balance


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2006, 5:15 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Looks good. If this new version is superior to the old one, it's usually a good idea to edit your top post to contain the new version so that visitors don't have to scroll down so far to find it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2006, 9:59 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
1uvaknd wrote:
Here is the updated code based on toralf's suggestions. Better?
Yes, it is better now.

You could still simplify/tweak the code a bit, remove not neccessary code line, combine lines, etc. But that is more cosmetic.

You haven't made changes as I suggested in b). But That's ok, if speed isn't an issue for you.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2006, 4:03 pm 
Thanks.
I tought I had made changes in B) by removing the FileAppend from my loops and putting the results in a variable.
Since I need the list to reflect the changes right away as soon as they are made, I'm not sure how else to handle the edits and deletes without accessing the file. Can you be more specific?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2006, 5:17 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Something like this:
Code:
#NoTrayIcon
#SingleInstance
F1=ADRESS.CSV
TestString = col1,col2,col3,col4`nname,email,url,program
IfNotExist, %F1%
  FileAppend,%TestString%, %F1%

Loop,Read,%F1%
  {
    Line%A_Index% := A_LoopReadLine
    Line0 = %A_Index%
  }

Gui,Add, ListView,grid r20 w780 -Multi +hscroll altsubmit vMyListView gMyListView, Name|EMAIL|URL|PROGRAM|ID
Gui, Add, Button, xm Section,AddName
Gui, Add, Button, ys gDELETE,DeleteName
Gui, Show, ,LISTVIEW
GoSub,FILLLIST
return

FILLLIST:
  LV_Delete()
  Loop, %Line0%
    {   
      StringSplit, Array, Line%A_Index%, `,
      LV_Add("",Array1,Array2,Array3,Array4,A_Index)
    }
  LV_ModifyCol(1,"AutoHdr")
  LV_ModifyCol(2,"AutoHdr")
  LV_ModifyCol(3,"AutoHdr")
  LV_ModifyCol(4,"AutoHdr")
  LV_ModifyCol(5,"0 Integer")
return

MyListView:
  if A_GuiEvent = DoubleClick
    {
      RowNumber := LV_GetNext(0)
      if not RowNumber
          return
      Loop, 5
          LV_GetText(A3%A_Index%, RowNumber,A_Index)
      editadd = edit
      GoSub,SMGUI
    }
return

SMGUI:
  Gui,2:Add,Text, xm Section w80, NAME
  Gui,2:Add,Edit, ys-4 w420  vA41, %A31%
  Gui,2:Add,Text, xm Section w80  , EMAIL
  Gui,2:Add,Edit, ys-4 w420  vA42, %A32%
  Gui,2:Add,Text, xm Section w80  , WWW
  Gui,2:Add,Edit, ys-4 w420  vA43, %A33%
  Gui,2:Add,Text, xm Section w80  , PROGRAM
  Gui,2:Add,Edit, ys-4 w420  vA44, %A34%
  Gui,2:Add, Button, xm, OK
  Gui,2:Show, , NAME-INPUT
return
 
2ButtonOK:
2GuiClose:
2GuiEscape:
  Gui,2:Submit
  Gui,2:Destroy
  Gui,1:Default
  if editadd = edit
      Line%A35% := A41 . "," . A42 . "," . A43 . "," . A44
  Else
    {
      Line0++
      Line%Line0% := A41 . "," . A42 . "," . A43 . "," . A44
    }
  GoSub,FILLLIST
return

DELETE:
  RowNumber := LV_GetNext(0)
  if not RowNumber
      Return
  Loop, 5
      LV_GetText(A3%A_Index%, RowNumber,A_Index)
 
  Msgbox,4,, Are you sure to delete ?
  Ifmsgbox, No
      return
 
 
  Loop, % Line0 - A35 + 1
    {
      ID := A_Index + A35 - 1
      IDNext := ID + 1
      Line%ID% := Line%IDNext%
    }
  Line0--
  GoSub,FILLLIST
return

ButtonAddName:
  A31=
  A32=
  A33=
  A34=
  editadd=add
  GoSub,SMGUI
return

GuiClose:
  FileContent =
  Loop, %Line0%
      FileContent := FileContent . Line%A_Index% . "`n"
  FileDelete, %F1%
  FileAppend, %FileContent%, %F1%
  Sleep, 500
  ExitApp
Only once read from file and only once written to file. The rest is done in memory. And it is 45 lines shorter.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2006, 2:36 am 
Offline

Joined: November 26th, 2005, 2:58 am
Posts: 6
Location: Ottawa, Canada
wow, thanks.
Just goes to show how much I have to learn. It's all great fun though.

I appreciate the help.

_________________
It's all about balance


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 10th, 2008, 1:11 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
Hi all

I have adapted this script to work for when you have greater than 10 fields in a row. This version works for an 11 field row.

Code:
; http://www.autohotkey.com/forum/viewtopic.php?t=7483&highlight=csv+listview
#NoTrayIcon
#SingleInstance
F1=attach_20080606_cba_2_suspense_pool.csv
TestString = col1,col2,col3,col4`nname,email,url,program
IfNotExist, %F1%
  FileAppend,%TestString%, %F1%

Loop,Read,%F1%
  {
    Line%A_Index% := A_LoopReadLine
    Line0 = %A_Index%
  }

Gui,Add, ListView,grid r25 w900 -Multi +hscroll altsubmit vMyListView gMyListView, Col1|Col2|Col3|Col4|col5|col6|col7|col8|col9|col10|col11|ID
Gui, Add, Button, xm Section,AddName
Gui, Add, Button, ys gDELETE,DeleteName
Gui, Show, ,Below are todays BTS Adjustments
GoSub,FILLLIST
return

FILLLIST:
   LV_Delete()
   Loop, %Line0%
      {
      ;Line%A_Index%:= RegExReplace(Line%A_Index%, "[^a-zA-Z0-9,-\s\.\+]") ; Replace all characters that are NOT alpha, numeric, a space, a comma or a dot
      StringSplit, Array, Line%A_Index%, `,
      LV_Add("",Array1,Array2,Array3,Array4,Array5,Array6,Array7,Array8,Array9,Array10,Array11,A_Index)
      }
  LV_ModifyCol(1,"AutoHdr")
  LV_ModifyCol(2,"AutoHdr")
  LV_ModifyCol(3,"AutoHdr")
  LV_ModifyCol(4,"AutoHdr")
  LV_ModifyCol(5,"AutoHdr")
  LV_ModifyCol(6,"AutoHdr")
  LV_ModifyCol(7,"AutoHdr")
  LV_ModifyCol(8,"AutoHdr")
  LV_ModifyCol(9,"AutoHdr")
  LV_ModifyCol(10,"AutoHdr")
  LV_ModifyCol(11,"AutoHdr")
  LV_ModifyCol(12,"0 Integer")
return

MyListView:
  if A_GuiEvent = DoubleClick
    {
      RowNumber := LV_GetNext(0)
      if not RowNumber
          return
      Loop, 12
          LV_GetText(A%A_Index%, RowNumber,A_Index)
      editadd = edit
      GoSub,SMGUI
    }
return

SMGUI:
  Gui,2:Add,Text, xm Section w80, Collecting BSB
  Gui,2:Add,Edit, ys-4 w420 readonly vB1, %A1%
  Gui,2:Add,Text, xm Section w80  , DRN
  Gui,2:Add,Edit, ys-4 w420 readonly vB2, %A2%
  Gui,2:Add,Text, xm Section w80  , Original Amount
  Gui,2:Add,Edit, ys-4 w420 readonly vB3, %A3%
  Gui,2:Add,Text, xm Section w80  , New Amount
  Gui,2:Add,Edit, ys-4 w420 readonly vB4, %A4%
  Gui,2:Add,Text, xm Section w80  , Adjust amt
  Gui,2:Add,Edit, ys-4 w420 readonly vB5, %A5%
  Gui,2:Add,Text, xm Section w80  , Comments
  Gui,2:Add,Edit, ys-4 w420  vB6, %A6%
  Gui,2:Add,Text, xm Section w80  , Date/Time
  Gui,2:Add,Edit, ys-4 w420 readonly vB7, %A7%
  Gui,2:Add,Text, xm Section w80  , WorkFlow
  Gui,2:Add,Edit, ys-4 w420 readonly vB8, %A8%
  Gui,2:Add,Text, xm Section w80  , Operator
  Gui,2:Add,Edit, ys-4 w420 readonly vB9, %A9%
  Gui,2:Add,Text, xm Section w80  , IDT
  Gui,2:Add,Edit, ys-4 w420 readonly vB10, %A10%
  Gui,2:Add,Text, xm Section w80  , Tray-Pkt-Location
  Gui,2:Add,Edit, ys-4 w420 readonly vB11, %A11%
  Gui,2:Add, Button, xm, OK
  Gui,2:Show, , Correct the values for this record 
return
 
2ButtonOK:
2GuiClose:
2GuiEscape:
  Gui,2:Submit
  Gui,2:Destroy
  Gui,1:Default
  if editadd = edit
      Line%A12% := B1 . "," . B2 . "," . B3 . "," . B4 . "," . B5 . "," . B6 . "," . B7 . "," . B8 . "," . B9 . "," . B10 . "," . B11 . "," . B12
  Else
    {
      Line0++
      Line%Line0% := B1 . "," . B2 . "," . B3 . "," . B4 . "," . B5 . "," . B6 . "," . B7 . "," . B8 . "," . B9 . "," . B10 . "," . B11 . "," . B12
    }
  GoSub,FILLLIST
return

DELETE:
  RowNumber := LV_GetNext(0)
  if not RowNumber
      Return
  Loop, 12
      LV_GetText(A%A_Index%, RowNumber,A_Index)
 
  Msgbox,4,, Are you sure to delete ?
  Ifmsgbox, No
      return
 
 
  Loop, % Line0 - A12 + 1
    {
      ID := A_Index + A12 - 1
      IDNext := ID + 1
      Line%ID% := Line%IDNext%
    }
  Line0--
  GoSub,FILLLIST
return

ButtonAddName:
  A1=
  A2=
  A3=
  A4=
  A5=
  A6=
  A7=
  A8=
  A9=
  A10=
  A11=
  editadd=add
  GoSub,SMGUI
return

GuiClose:
  FileContent =
  Loop, %Line0%
      FileContent := FileContent . Line%A_Index% . "`n"
  FileDelete, %F1%
  FileAppend, %FileContent%, %F1%
  Sleep, 500
  ExitApp


Just thought I would share. Thanks to the original poster.

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 28th, 2008, 9:18 am 
Offline

Joined: December 22nd, 2006, 1:41 pm
Posts: 43
Hi poo_noo

Great script.

One problem: If I type in at text like this "Hello world, how are you?"

The script thinks that the " , " is a breakline in the CSV file and also when the GUI updates with the new cols.

Any workarounds on this issue?

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2009, 3:23 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
Hi Deicer

Sorry, I didn't see your question. My post was an adaption of the original script but I can have a look to at the code for a solution.

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2009, 3:44 am 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
I dont get what this is suposed to do. It just shows the values but doesn't let you change them. When you try to add a new item it doesn't let you type anything except comments.

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2009, 4:36 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
Hi

Yes it has been customised by me for use at work. I only ever wanted the end users to edit one field only. To make all the fields editable, just remove the readonly option for all the EDIT fields in GUI 2.

_________________
Paul O


Last edited by poo_noo on January 14th, 2009, 9:43 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 25 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group