How to enter content to an existing CSV file - visual-foxpro

I am trying to enter data cell by cell to a CSV file, but when I open it, I get all the contents in column A of the CSV file separated by "," (comma).
I am trying it in the following way:
oExcel = CREATEOBJECT("Excel.Application")
oWorkBook=oExcel.Workbooks.Open("c:\planilla.csv")
oExcel.application.Cells(1,1).Value = "StockCode"
oExcel.application.Cells(1,2).Value = "In-Stock"
oExcel.application.Cells(1,3).Value = "Kit"
oWorkbook.Save()
oWorkbook.Close()
oExcel.Quit()
RELEASE oWorkbook
RELEASE oExcel
When opening the CSV document, the output is in a single cell (column A), like this:
StockCode, In-Stock, Kit.
I hope you can help me, regards

First off, I don't understand why you are trying to use the Excel object in order to create a csv file. Why don't you just create a csv file with the commands in VFP?
Second, I did try your example and it did create one cell with the 3 items separated by 2 tabs. But, if I do the same thing on an xlsx file (an actual Excel workbook), your example works. I don't think the Excel office automation object was ever intended to be used on a CSV file.
If you need to output to a CSV file, you can do that with VFP commands without having to create an Excel object.
Saw your comment... try running this in VFP:
* create cursor and some records
CREATE CURSOR test (nid i, stockcode c(10),instock i,kit c(20), lincsv l)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (1,"SC-001",10,"kit001",.f.)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (2,"SC-010",0,"kit003",.f.)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (3,"SC-200",5,"kit010",.f.)
* call function to add records to csv file
=AddToCSV()
* insert a couple more records to cursor
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (4,"SC-201",6,"kit001",.f.)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (5,"SC-005",520,"kit099",.f.)
* call function again to add new records to existing csv file
=AddToCSV()
FUNCTION AddToCSV
* open csv file (create if it doesn't exist, add to existing)
SET ALTERNATE TO c:\temp\planilla.csv ADDITIVE
SET ALTERNATE ON
SCAN FOR NOT linCSV
?? stockcode + ","
?? instock
?? "," + kit
replace lincsv WITH .t.
?
ENDSCAN
SET ALTERNATE OFF
SET ALTERNATE TO
RETURN

Related

How to use DWitemstatus in Power Builder

I'm learning about Power Builder, and i don't know how to use these, (DWitemstatus, getnextmodified, modifiedcount, getitemstatus, NotModified!, DataModified!, New!, NewModified!)
please help me.
Thanks for read !
These relate to the status of rows in a datawindow. Generally the rows are retrieved from a database but this doesn't always have to be the case - data can be imported from a text file, XML, JSON, etc. as well.
DWItemstatus - these values are constants and describe how the data would be changed in the database.
Values are:
NotModified! - data unchanged since retrieved
DataModified! - data in one or more columns has changed
New! - row is new but no values have been assigned
NewModifed! - row is new and at least one value has been assigned to a column.
So in terms of SQL, a row which is not modified would not generate any SQL to the DBMS. A DataModified row would typically generate an UPDATE statement. New and NewModifed would typically generate INSERT statements.
GetNextModifed is a method to search a set of rows in a datawindow to find the modified rows within that set. The method takes a buffer parameter and a row parameter. The datawindow buffers are Primary!, Filter!, and Delete!. In general you would only look at the Primary buffer.
ModifedCount is a method to determine the number of rows which have been modifed in a datawindow. Note that deleting a row is not considered a modification. To find the number of rows deleted use the DeletedCount method.
GetItemStatus is a method to get the status of column within a row in a data set in a datawindow. It takes the parameters row, column (name or number), and DWBuffer.
So now an example of using this:
// loop through rows checking for changes
IF dw_dash.Modifiedcount() > 0 THEN
ll = dw_dash.GetNextModified(0,Primary!)
ldw = dw_dash
DO WHILE ll > 0
// watch value changed
IF ldw.GetItemStatus(ll,'watch',Primary!) = DataModified! THEN
event we_post_item(ll, 'watch', ldw)
END IF
// followup value changed
IF ldw.GetItemStatus(ll,'followupdate',Primary!) = DataModified! THEN
event we_post_item(ll, 'followupdate', ldw)
END IF
ll = ldw.GetNextModified(ll,Primary!)
LOOP
ldw.resetupdate() //reset the modifed flags
END IF
In this example we first check to see if any row in the datawindow has been modified. Then we get the first modified row and check if either the 'watch' or 'followupdate' columns were changed. If they were we trigger an event to do something. We then loop to the next modified row and so on. Finally we reset the modified flags so the row would now show as not being mofified.

How do I add data in a new column on each row within a CSV?

So I have a method that looks like this:
csvs = Dir["#{#dir_name}/#{#state}/*.csv"]
csvs.each do |csv|
city = csv.split(/[\/]|.csv-updated|.csv/).last
CSV.foreach(csv, headers: true) do |row|
temp = extract_email(row['Website'])
# add result from temp above to existing row
end
end
Basically what I want to do is, on each row that I process within the CSV, I want to do something with the data in row['Website'] (as represented as temp in the snippet above), and then I want to take the result and add it as new columns to that row.
So basically I want to append to a each row. How do I do that?
I figured this out.
Basically, I can't edit an existing CSV like that, so I just have to read all the rows from the old_csv and create a new_csv. From that old_csv, I then pass the corresponding data from the old_csv to the method I want (i.e. extract_email(row['Website']) and then I process that info and collect the emails in an array.
From there, all I have to do is iterate over the collection of temp and add it to the row object:
temp.each { |t| row << t }
Then I just add row to the new CSV, which will include the old row + the newly added columns. So in essence I am expanding each row.

insert a new line of row into grid without recordsource

I've some problem to insert a new row line into record without insert into cursor and then append into recordsource.
That said i've five record already in existing grid list but i need to add a new line into next row under grid list.
Here is my code:
vc_tmpfile = alltrim(sys(3)) + ".dbf"
create table &vc_tmpfile (text c(254))
append from C:\tmp\aaa.out type sdf
dele all for len(ALLTRIM(text)) < 15
pack
with thisform.grid_list
Do while !EOF()
if alltrim(substr(text,1,4)) == "POPL"
.columns(2).text1.value = alltrim(substr(text,6,6))--->>It shows nothing after insert
endif
skip
enddo
endwith
Appreciate thankful to someone could help.
The VFP Grid control displays data from its RecordSource, and that one is always an "alias".
So the easy VFP way is simply to do an SQL Insert Into yourAlias ..., or use the xBase Append and Replace ... commands.
In your scenario, the Grid.RecordSource probably is the temp table you created, where your code would get improved if you'd use the Create Cursor ... command for creating the temporary table, and by avoiding the infamous & operator and using (name) expressions instead wherever you can
Looks like this is what you are trying to do:
thisform.grid_list.RecordSource = ''
vc_tmpfile = Sys(2015)
Create cursor (m.vc_tmpfile) (text c(254))
append from ('C:\tmp\aaa.out') type sdf for len(ALLTRIM(text)) >= 15
Update (m.vc_tmpFile) set text = alltrim(substr(text,6,6)) where text like "POPL%"
locate
thisform.grid_list.RecordSource = m.vc_tmpfile
How did the existing 5 records get into the grid with no recordsource?
You should be updating a record source instead of updating the grid controls...
Maybe take this approach...
1) Stick data into array or temp readwrite cursor
2) Set grid recordsource to the array or cursor
3) Refresh Grid

Access word documents header containing a table with vbscript

I need to read the pure text values from cells of a table located in the page header of a word document using vbscript.
All i could achieve until now is to read some kind of text from the header by this:
wscript.echo WordApp.ActiveDocument.Sections(1).Headers(1).Range.FormattedText.Text
But how can i gain access to the table object and read each cell value?
Tables in Word documents can be enumerated via the Tables collection.
For Each tbl In WordApp.ActiveDocument.Sections(1).Headers(1).Range.Tables
For i = 1 To tbl.Rows.Count
For j = 1 To tbl.Columns.Count
WScript.Echo tbl.Cell(i, j).Value
Next
Next
Next

Tables got over-written

I want to loop thru a dbf and create word table for each record meeting the condition, and I got a one-page report with only the last rec in a single table. Look like all records are written to the same table. I tried to use n = n + 1 to place the variable as an element to the table
oTable = oDoc.tables[n]
But seems it only support numerical rather than variable ?
You have to add each table as you go, making sure to leave space in between them (because Word likes to combine tables).
You'll need something like this inside your loop:
* Assumes you start with oDoc pointing to the document,
* oRange set to an empty range at the beginning of the area where you want to add the tables,
* and that nRows and nCols give you the size of the table.
oTable = oDoc.Tables.Add(m.oRange, m.nRows, m.nCols)
oRange = oTable.Range()
oRange.Collapse(0)
oRange.InsertParagraphAfter()
oRange.Collapse(0)
After this code, you can use oTable to add the data you want to add. Then, on the next time through the loop, you're ready to add another table below the one you just filled.

Resources