Jump to content

Variable is empty but it isnt - (Moved)


raulya
 Share

Recommended Posts

Local $data1 = _StringBetween($csv_line_values[4], "string1", "string2")
    Msgbox(0,'data1:', $data1)
    _Excel_RangeWrite($oWorkbook, Default, $data1, "G" & $x-1)

Hello, first time here.

I am writing a script to copy some data into an excel file. My first problem was when I wanted to add 2 floats. I observed that the final result is not ok and I started to debug when I saw this 'issue?'

I have the code from up^. When I execute the script the messagebox it will be empty, even though in the excel is writing the good value. What is wrong?

Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Please post a script that demonstrates the issue when we run it or else it will be hard to assist.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Oh, sorry for wrong post.

Well, this is the whole script:

#include <Array.au3>
#include <IE.au3>
#include <file.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <String.au3>


Global $csv_file = "D:\autoit\Test1\tranzactii.csv"
Global $aRecords
Global $ExcelFileLocation

Local $oExcel

If Not _FileReadToArray($csv_file,$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array error:" & @error)
   Exit
EndIf

    $oExcel = _Excel_Open()
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    ; Create a new workbook, write some data and save it to @tempdir
    ; Create the new workbook
    Local $oWorkbook = _Excel_BookNew($oExcel)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSave Example 1", "Error creating new workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

For $x = 2 to $aRecords[0]
    ;Msgbox(0,'Record:' & $x, $aRecords[$x]) ; Shows the line that was read from file

    $csv_line_values = StringSplit($aRecords[$x], ",",1) ; Splits the line into 2 or more variables and puts them in an array
    ;_ArrayDisplay($csv_line_values) ; Shows what's in the array you just created.

    ;Msgbox(0,'Record:', $csv_line_values[0])
    $csv_line_values[1] = StringReplace($csv_line_values[1], '"', '')
    $csv_line_values[2] = StringReplace($csv_line_values[2], '"', '')
    $csv_line_values[3] = StringReplace($csv_line_values[3], '"', '')
    $csv_line_values[4] = StringReplace($csv_line_values[4], '"', '')
    $csv_line_values[5] = StringReplace($csv_line_values[5], '"', '')

    ; Write in excells
    _Excel_RangeWrite($oWorkbook, Default, $csv_line_values[1], "A" & $x-1)
    _Excel_RangeWrite($oWorkbook, Default, $csv_line_values[2], "B" & $x-1)
    _Excel_RangeWrite($oWorkbook, Default, $csv_line_values[3], "C" & $x-1)
    _Excel_RangeWrite($oWorkbook, Default, $csv_line_values[4], "D" & $x-1)
    _Excel_RangeWrite($oWorkbook, Default, $csv_line_values[5], "E" & $x-1)

    ;write AMMOUNT BTC extracted (net)
    Local $data1 = _StringBetween($csv_line_values[4], "gincoin9 sold", "GIN")
    ConsoleWrite("data1: " & $data1)
    _Excel_RangeWrite($oWorkbook, Default, $data1, "G" & $x-1)


    ;write FEE BTC extracted
    Local $data2 = StringTrimRight($csv_line_values[5], 3)
    _Excel_RangeWrite($oWorkbook, Default, $data2, "J" & $x-1)

    ;write ER GIN/BTC
    Local $data3 = _StringBetween($csv_line_values[4], "at", "BTC")
    _Excel_RangeWrite($oWorkbook, Default, $data3, "M" & $x-1)

    ;write TOTAL AMMOUNT BTC, including commission
    Local $data4 = $data1 + &data2
    _Excel_RangeWrite($oWorkbook, Default, $data4, "H" & $x-1)
    Msgbox(0,'data4:', $data4) 

    If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Exit SetError(1, 0, 0)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSave Example 1", "Error writing to cell 'A1'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Next

    _Excel_BookSaveAs($oWorkbook, @ScriptDir & "\_Excel.xls", Default, True)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSave Example 1", "Error saving workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

My first issue was at line:

Local $data4 = $data1 + $data2

I tried to make any operations but because they are string (i assume) it does not work.

And then I tried to check the data1 and data2 in order to try to convert them to floats but I ran into this problem:

Into the excel are correctly writed but in console they are empty:

Local $data1 = _StringBetween($csv_line_values[4], "gincoin9 sold", "GIN")
    ConsoleWrite("data1: " & $data1)
    _Excel_RangeWrite($oWorkbook, Default, $data1, "G" & $x-1)

 

Link to comment
Share on other sites

_StringBetween returns an array, you can't display it in a ConsoleWrite like that, try _DebugArrayDisplay instead to see what you've got in the returned results.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

19 minutes ago, raulya said:

mathematical operations I need to convert them to float, right?

you can use number () func to translate string but make sure those contain only numeric characters...

Link to comment
Share on other sites

46 minutes ago, raulya said:

i tried with data1[0] + data2[0] but I receive errors

$data1 is an array, and $data2 is a simple string variable coming from the return of StringTrimRight, you can't add it as an array element.

BTW, when your code comes back with errors, don't just say you receive errors, tell us what those error messages are so we don't have to reinvent the wheel.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...