Jump to content
Sign in to follow this  
andomatic

Append an excel sheet from first blank row

Recommended Posts

andomatic

Hi,

I've some data that I need to write into an existing excel sheet.The sheet has 24 active columns (a-x) and some rows populated, 30 or so. To get my data I read a locally saved htm file and populate some vars, no problem. The question is how to then find the first blank row, write my data, move to the next row, read the next data set and write it. I've got the code done for the data loop piece, just can not for the life of me figure out the excel. I don't care if I use the excel UDF or COM calls, just need a push in the right direction please... Here is what I have so far, any help is appreciated.

#include <Constants.au3>
#include <Excel.au3>
#include <Array.au3>
#include <Date.au3>
#include <File.au3>
#include <String.au3>

;WinSetState("Excel","",@SW_MINIMIZE)

Const $log = @ScriptDir & "\CreateDB.log"
Const $sSrcPath = @ScriptDir & "\result"
Const $xlPath = "\xxx\upload.xls"

Local $fTgtFile

Local $sCompName 
Local $sCompStreetAdd
Local $sCompCity 
Local $sCompState
Local $sCompZip 
Local $sCompCounrty 
Local $sCompTele

Local $sNAICS 
Local $sNAICSDesc 

Local $sSIC2
Local $sSIC4 
Local $sSICM 
Local $sSIC2Desc 
Local $sSIC4Desc 
Local $sSICMDesc 

Local $sMemberVar 
Local $sMemberTitle
Local $sMemberFunc 
Local $sMemberPosition 
Local $sMemberSIC2 
Local $sMemberSIC4 
Local $sMemberSICM 
Local $sMemberRev 
Local $sMemberEmp

_FileWriteLog($log, "***Run Start***")

Local $oExcel = _ExcelBookOpen($xlPath) ;Setup Excel
WinSetState("Excel","",@SW_MAXIMIZE)
Sleep (1000)
_ExcelSheetActivate($oExcel, "Sheet1")
Local $iRows = $oExcel.ActiveSheet.UsedRange.Rows.Count

Local $aHandle = _FileListToArray($sSrcPath, "*.htm",1)

; Work through the folder
If @error Then
    MsgBox(0, "Error", "No files matched")
Else
    ; Loop through the found files
    For $i = 1 To 5 ;$aHandle[0]
        MsgBox(4096, "File:", $aHandle[$i])
        $fTgtFile = FileOpen($sSrcPath & "\" & $aHandle[$i],0)
        If $fTgtFile = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        EndIf

        While 1 ;MsgBox(0, "Company City:", $sCompCity[0])
            Local $line = FileReadLine($fTgtFile)
            If @error = -1 Then ExitLoop

            ;Company Name
            If StringInStr ($line,'companyName: "') Then
                $sCompName = _StringBetween($line,'companyName: "','",')
            EndIf

             ;Company Street
             If StringInStr ($line,'itemprop="streetAddress">') Then
                $sCompStreetAdd = _StringBetween($line,'itemprop="streetAddress">','</div>')
            EndIf

             ;Company City
             If StringInStr ($line,'city: "') Then
                $sCompCity = _StringBetween($line,'city: "','",')
            EndIf

             ;Company State
             If StringInStr ($line,'state: "') Then
                $sCompState = _StringBetween($line,'state: "','",')
            EndIf

             ;Company Zip
             If StringInStr ($line,'<span class="addresspostalCode" itemprop="postalCode">') Then
                $sCompZip =  _StringBetween($line,'itemprop="postalCode">','</span>')
            EndIf

            ;Company Country
             If StringInStr ($line,'"addressCountry" class="">') Then
                $sCompCountry =  _StringBetween($line,'class="">','<!--country-->')
            EndIf

            ;Company Telephone
             If StringInStr ($line,'itemprop="telephone">') Then
                $sCompTele =  _StringBetween($line,'itemprop="telephone">','</dd>')
            EndIf

            ;NACIS
             If StringInStr ($line,'naics":"') Then
                $sNAICS =  _StringBetween($line,'naics":"','"}')
            EndIf

            ;NACIS Desc
             If StringInStr ($line,'naics_desc":"') Then
                $sNAICSDesc =  _StringBetween($line,'naics_desc":"','"}')
            EndIf

            ;SIC2
            If StringInStr($line,'{"sic2":') Then
                $sSIC2 = _StringBetween($line,'{"sic2":','}')
            EndIf

            ;SIC4
            If StringInStr($line,'{"sic4":') Then
                $sSIC4 = _StringBetween($line,'{"sic4":','}')
            EndIf

            ;SIC4
            If StringInStr($line,'{"sicm":') Then
                $sSICM = _StringBetween($line,'{"sicm":','}')
            EndIf

            ;SIC2Desc
            If StringInStr($line,'{"sic2_desc":') Then
                $sSICM = _StringBetween($line,'{"sic2_desc":','}')
            EndIf

            ;SIC4Desc
            If StringInStr($line,'{"sic4_desc":') Then
                $sSICM = _StringBetween($line,'{"sic4_desc":','}')
            EndIf

            ;SICM_Desc
            If StringInStr($line,'{"sicm_desc":') Then
                $sSICM = _StringBetween($line,'{"sicm_desc":','}')
            EndIf

            ;Member Var
            If StringInStr($line,'{"member_s_var":') Then
                $sMemberVar= _StringBetween($line,'{"member_s_var":','}')
            EndIf

            ;Member Title
            If StringInStr($line,'member_attrs_title":"') Then
                $sMemberVar= _StringBetween($line,'member_attrs_title":"','}')
            EndIf

            ;Member Function
            If StringInStr($line,'{"member_function":"') Then
                $sMemberFunc= _StringBetween($line,'{"member_function":"','}')
            EndIf

            ;Member Position
            If StringInStr($line,'{"member_position":"') Then
                $sMemberPosition= _StringBetween($line,'{"member_position":"','}')
            EndIf

            ;Member SIC2
            If StringInStr($line,'{"member_sic2":"') Then
                $sMemberSIC2= _StringBetween($line,'{"member_sic2":"','}')
            EndIf

            ;Member SIC4
            If StringInStr($line,'{"member_sic4":') Then
                $sMemberSIC4= _StringBetween($line,'{"member_sic4":','}')
            EndIf

            ;Member SICM
            If StringInStr($line,'{"member_sicm":') Then
                $sMemberSICM= _StringBetween($line,'{"member_sicm":','}')
            EndIf

            ;Member Revenue
            If StringInStr($line,'{"member_revenue":"') Then
                $sMemberRev= _StringBetween($line,'{"member_revenue":','}')
            EndIf

            ;Member Employees
            If StringInStr($line,'{"member_employees":"') Then
                $sMemberRev= _StringBetween($line,'{"member_employees":','}')
            EndIf
;Stuck here on how to get those vars into excel :(


        WEnd


    Next
EndIf



Exit


; Open the workbook
;~ $oExcel = ObjCreate("Excel.Application")

;~ $oExcel = ObjCreate("Excel.Application")
;~ $oBook = $oExcel.Workbooks.Open("xxx\upload.xls")
;~ $oExcel.Visible = True
;~ $oSheet = $oBook.Worksheets(1)
;~ $oSheet.Activate
;~ $oRange = $oSheet.UsedRange
;~ $oRange.SpecialCells($xlCellTypeLastCell).Activate
;~ $newRow = $oExcel.ActiveCell.Row + 1
;~ $oExcel.Range("A" & $newRow).Activate

Share this post


Link to post
Share on other sites
andomatic

Bill,

Thanks for the link, I've incorporated the cursor move piece and it seems to work, to write in the data, I'm thinking that I need to create an array out of my info and then somehow write that across rather than down so to speak

Share this post


Link to post
Share on other sites
l3ill

If you have loops already setup its just a matter of adding the count variable (move to next cell) and adding

_ExcelWriteCell($oExcel, $i, $i, 3)

or similar..

The variables ($i) (Position to add data to excel sheet) can be controlled by a For To Next Loop.

BTW moving down is default in excel

Edited by billo

Share this post


Link to post
Share on other sites
water

You can copy it in a single statement. Use the "Usedrange" object.

This has been answered a few weeks ago. I will post the link ... as soon as I can find it ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

UsedRange covers the range from the first row with data to the last row with data.

>Here you find an example using my rewrite of the Excel UDF. It's much faster than the UDF currently distributed with AutoIt.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • ajblandford
      By ajblandford
      I have embeded an Excel file in my autoit Script and want to use it as part of my GUI. I want to be able to select a range of cells and add the cell data to an edit box. Then dump that data to an array. I am using GUICtrlCreateObj to embed the spreadsheet. I cannot find any way to let my program see what cells are active so I can add that data to the edit box. 
       
      #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <excel.au3> $FileName = 'C:\VLog\book1.xlsx' If Not FileExists($FileName) Then MsgBox(0, "ERROR", "File not found") Exit EndIf ;Basic GUI $oExcelDoc = ObjGet($FileName) ; Get an excel Object from an existing filename If IsObj($oExcelDoc) Then $mainGUI = GUICreate("Production Room 2", 1800, 1200, 10, 10, $WS_MINIMIZEBOX + $WS_SYSMENU + $WS_CLIPCHILDREN) $GUI_ActiveX = GUICtrlCreateObj($oExcelDoc, 10, 70, @DesktopWidth - 250, @DesktopHeight - 260) $data = GUICtrlCreateEdit("", @DesktopWidth - 225, 75, 100) $btn = GUICtrlCreateButton( "GO", @DesktopWidth - 100, 75, 75) Else MsgBox(0, "", "failed") EndIf ;------------------ ;Turns off all command bars in excel to prevent user from making changes For $Bar In $oExcelDoc.CommandBars If $Bar.Enabled = True Then $Bar.Enabled = False If $Bar.Visible = True Then $Bar.Visible = False Next $oExcelDoc.Application.DisplayFormulaBar = False $oExcelDoc.Application.CommandBars("Shadow Settings").Visible = False $oExcelDoc.Application.DisplayScrollBars = True $oExcelDoc.Application.DisplayStatusBar = False GUISetState()  
    • Skeletor
      By Skeletor
      Hi Guys,
      Can anyone point me in the right direction. I'm trying to accomplished conditional formatting with arrows, but the code I have is wrong... 
      .Range("=$A3:$A4000").FormatConditions.Add = (xlCellValue, xlGreater, "=$A:$A") .IconSet(xl3Arrows) ;ActiveWorkbook.iconsets I also tried it like this:
      .Range("=$A3:$A4000").FormatConditions _ .Add(xlCellValue, xlGreater, "=$A:$A") .IconSet(xl3Arrows) ;ActiveWorkbook.iconsets  
    • Skeletor
      By Skeletor
      Hi Guys,
      How would you use _Excel_FilterSet to filter the excel sheet from largest number to smallest number?
      _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">20", 1, "<40") ; OR _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">", 1, "<") Excel Sheet attached. Trying to filter on the last column "I2"
      Inventory.xlsx
    • Skeletor
      By Skeletor
      Hi All,
      Has anyone come across this before?
      Code below:
      $SheetList = _Excel_SheetList($oWorkbook) _FileWriteFromArray("C:\" & $SheetListOutput, $SheetList, 1) Result:
      ResultABC| ResultDEF| ResultGHI| ResultJKL| ResultMNO| It created these "|" vertical bars?
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
×