LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Duplicate Write to Spreadsheet

Hello,

 

The attached snippet of code contains a stacked sequence structure.

 

The first frame writes the values of two Booleans and an alphanumeric string to the 1st row on a spreadsheet. 

 

The second frame should write the value of a third Boolean to the 2nd row of the spreadsheet, but instead it rewrites the data that was written in the 1st row in the 2nd row.

 

Why isn't this code working properly?

 

Thanks,

 

Drew

0 Kudos
Message 1 of 14
(4,717 Views)
It looks like you made the same mistake as before. In your second frame, you've got the write to spreadsheet set to append.
0 Kudos
Message 2 of 14
(4,707 Views)
You are always reading the entire spreadsheet, replace a line, then rewrite it. In this case, you should NOT use append, else your file size will snowball.
 
In addition, if there is only one set of data in the file, you cannot use replace array subset for the second set, because there is nothing to replace. You need to test the array size to decide if you should replace a subset or append to the array.
0 Kudos
Message 3 of 14
(4,703 Views)
In the 2nd frame, you are sending in the entire 2D array.  Of course it will append all data to the spreadsheet, including the 1st row.  The second row in the array should be in the third row in the spreadsheet according to your code.    Here is what your code is doing:
You write a 1D array if the file is new to spreasheet row 1.  If the file is not new, you read it and rewrite it with new data appended.  Because you have append=false, the entire spreadsheet gets rewritten.
In frame 2, you read in the entire spreadsheet.  Then you append the entire spreadsheet again into you file because append=true.
 
You should either rewrite the entire array to the spreadsheet with the append input set to false, or you should write only the latest row with append set to true.  In the second case, there is no need to read the spreadsheet at all.  Just write the new data with append = true.  The old data will still be there.
Also, instead of recreating the file path everywhere, wire the path output from the first time you built it into the error/no error case structure.  Use a sequence local and wire the path to it in the first frame.  In the second frame wire from the sequence local to the spreadsheet function input.
- tbob

Inventor of the WORM Global
0 Kudos
Message 4 of 14
(4,702 Views)
I tried to rewrite the entire array to the spreadsheet with the append input set to false.  In this case, the status of the third Boolean was not recorded in the spreadsheet.  I have attached this code, because I believe it to be the proper approach, but it is not working properly for some reason.  I may be missing something.
 
I tried to write only the latest row with append set to true.  In this case, whenever the row is rewritten, it is appended to the end of the file.  This is what I'm trying to avoid.  Anytime a row is rewritten, it must be rewritten in over the old data, in its specitied row.
 
It may be wise to test the array size and somehow decide if the program should replace an array subset or append to the array.  Can you send me a quick example of how to implement that?
 
Thanks,
 
Drew
 
 
 
0 Kudos
Message 5 of 14
(4,688 Views)
Don't use append.  Always set the append input to false in all cases (you have it set to true in the case where you create a new file, it should be false).  In frame 2, using Replace Array Subset does not put in the third value because there is no row 1 to replace.  You can't use this function.  When adding a new row, you must use Build Array.  You would need to check how many rows are in the array after you read the spreadsheet.  If the index of row you want to replace is greater than the last row of the array, you need to use Build Array.  If the index exists in the array, use Replace Array Subset.  Check the array size (function found in array palette), index the output to get the number of rows.  Subtract 1 from this number (row numbers begin with 0) and this is the last row #.  If the last row # is 0, you cannot replace row 1.  Check if your index is greater than the last row #.  If so, use Build Array or Insert Into Array.  Then, always write the entire array back into the spreadsheet with append=false.
- tbob

Inventor of the WORM Global
0 Kudos
Message 6 of 14
(4,682 Views)
How do I index the output of the array size function to get the number of rows?
0 Kudos
Message 7 of 14
(4,667 Views)
Hello again,
 
Here is a new set of code.  I believe I have implemented the changes you've suggested.
 
In the second frame of the sequence structure I try to index the row number.  Currently the Index 0 input of the Index Array function is wired to a constant that is set to 1.  In this case, the program writes only the first row of the spreadsheet.
 
If I set this constant to 0, the program only writes the 2nd row of the spreadsheet.
 
I don't think I'm indexing the row number correctly.
 
Why doesn't this program write both rows of the spreadsheet like it is supposed to do?    How can I fix it?  Please help!
 
Thanks,
 
Drew
0 Kudos
Message 8 of 14
(4,658 Views)
When usng array length with 2D arrays, element 0 is for the number of rows, element 1 is for the number of columns.  See attached vi.
- tbob

Inventor of the WORM Global
0 Kudos
Message 9 of 14
(4,651 Views)

That's what I thought.  The constant should be set to 0.  The problem with that is as I said before.

When you set the constant to 0, the program only writes the 2nd row of the spreadsheet.  Either that or it writes the 2nd row over the first row, I dont know which.  Either way, it is not working as it was designed.  Do you happen to know why this might be happening?

Thanks,

Drew

P.S.  Thank you for the code.  I very much appreciate you're help.

0 Kudos
Message 10 of 14
(4,638 Views)