BrandonSanchez

Incremental copy paste

24 posts in this topic

Hello Autoit community

I started using Autoit about 2 weeks ago and have found myself lost looking for information. Majority of the Help options are very easy to understand but I think that my general lack of knowledge about programming has slowed my progress. I tried very hard to not be that guy asking annoying questions on the forum but I’m afraid I’m out of options…

The steps below are my attempt to explain my scenario

Step 1: open excel file

Step 2: Read range of Excel column “A1” – “A???”) The range of rows will vary from sheet to sheet.

Step 3: take the values (one cell at a time) and place one value into my web browser ran program.

Step 4: if that value has been added before (indicated by the PixelSearch) than delete (represented by the mouse click) and start the loop over moving onto the next cell.

Step 5: if that value is new then submit the value (represented by the 2 mouse clicks at the end of the (if function)

The code listed below is as far as I got. I hope this explanation will suffice and I look forward to any help I can get.

Capture.JPG.e97e00fae45cf0dddc5e50b663d0

(From WinActive(“Paste Too”) down is working as intended.)

 

Share this post


Link to post
Share on other sites



You have a lot going on there ... I will give some high level feedback without re-writing your code.  First problem, the range of used cells in Excel varies.  To solve that problem you can use this:

$oRange = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeLastCell)

The next issue is the for loop.  I think you would want to read the cells into an array first using _Excel_RangeRead.   Once in the array, you can look for unique values using _ArrayUnique.  Then, after you have a unique array of values from your spreadsheet you can send them to your website in the for loop (your current approach goes for 10,000 times cell by cell every time - this would be much more efficient).  That would, based on your explanation, also eliminate the pixelsearch approach to finding duplicates after you send them (because there would be none before you send).

Hope some of that helps.

 


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

There are better ways to automate a website, with IE.au3 is delivered with AutoIt. I suggest use _IEDocInsertHTML, this is the better/safer solution.

Edit: it's only temporary.

Edited by AutoBert

Share this post


Link to post
Share on other sites

Thank you for the feedback. I will look into your suggestions and update with new information. 

As for the PixelSearch elimination. I receive a new worksheet every week that i will run through this code so since ArrayUnique cannot sort the data that was placed into my website from previous worksheets it will be need to prevent future repetitive inputs. Sorry for not clearing that up in my original explanation. 

Thank you again jfish.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You can also read Table from the webpage:

Quote

_IETableWriteToArray

Reads the contents of a Table into an array

Edited post#3:

1 hour ago, AutoBert said:

it's only temporary.

Look for _IEFormElementSetValue() and _IEFormSubmit.

Edited by AutoBert

Share this post


Link to post
Share on other sites

Hi @BrandonSanchez
Like @Jfish said you can do it in that way and about his high level recommendation I cant make that work and I end up with:

$aResult = _Excel_RangeRead($oWorkbook, Default,$oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeLastCell), 1)

But that only will give you last used cell on the entire Worksheet and not per row.

If the number of row will vary you can read the entire worksheet to an array with this code and then play with that array:

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $oExcel = _Excel_Open()
If @error Then
    MsgBox(0, "Error", "Error creating Excel object")
    _Excel_Close($oExcel)
    Exit
EndIf
 Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Excel1.xlsx", Default, Default)
 If @error Then
     MsgBox(0, "Error", "Error opening the workbook")
     _Excel_Close($oExcel)
     Exit
 EndIf
 Local $aResult = _Excel_RangeRead($oWorkbook, Default, Default, 1)
 If @error Then
     MsgBox(0, "Error", "Unable to read workbook")
     _Excel_BookClose($oWorkbook)
     Exit
 EndIf
 _ArrayDisplay($aResult, "Data", "")
 _Excel_BookClose($oWorkbook)

Note that this is not 1D array so you will need to make some test with _ArrayUnique() because the way to do it is not the same.

Regards
Alien.

Share this post


Link to post
Share on other sites

@alien4u I did not know the worksheet had more than one used column since the OP related to Col A onl but you should be able to swap out a specific range for

.UsedRange

And I think it would work for any given column instead of all of them.  


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
5 hours ago, Jfish said:

@alien4u I did not know the worksheet had more than one used column since the OP related to Col A onl but you should be able to swap out a specific range for

.UsedRange

And I think it would work for any given column instead of all of them.  

Hi @Jfish I tried that before and I cant find a way to use that per row or per columns, you can take a look to this:


If you know a way to do it or you could find I way will be very nice.
Thanks you.

Regards
Alien.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This post explains how to do it for a single column:

This is a working example:

#include<Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\testdata.xlsx") ; your workbook here

Global Const $xlUp = -4162  ;global constant for Excel up, finding last row (thanks Water ... pulled from your post)
With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "B").End($xlUp).row ; switch out your cols here
EndWith
MsgBox(0,"",$lastRow)

@water I did not notice $xlUp in the excel constants.  I know you could probably fill that file to a crazy size if you added everything but wondering if would consider adding it?

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
1 hour ago, Jfish said:

This post explains how to do it for a single column:

This is a working example:

#include<Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\testdata.xlsx") ; your workbook here

Global Const $xlUp = -4162  ;global constant for Excel up, finding last row (thanks Water ... pulled from your post)
With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "B").End($xlUp).row ; switch out your cols here
EndWith
MsgBox(0,"",$lastRow)

@water I did not notice $xlUp in the excel constants.  I know you could probably fill that file to a crazy size if you added everything but wondering if would consider adding it?

Hi @Jfish and thanks you again but I'm still unable to make that work per row and not per columns.
With this:

With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "B").End($xlUp).row ; switch out your cols here
EndWith

I'm able to get how much rows per columns or how much columns from A to H with this:

With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Columns.Count, "H").End($xlUp).column ; switch out your cols here
EndWith

I also try this to get last value per column but I still need last used cell per row, notice is not the same last used cell(last cell with a value) so I dont' need last cell per column or last cell per row, I need last used cell per row.

Global Const $xlUp = -4162  ;global constant for Excel up, finding last row
$LNC = $oWorkbook.ActiveSheet.Range("F65535").End($xlUp)
$sResult = _Excel_RangeRead($oWorkbook, Default, $LNC, 1)

Regards
Alien.
 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

The code I supplied was last used row in a col.  This is last used col in a row

;Find the last used column in a Row: row 1 in this example
Global Const $xlToLeft = -4159
With $oWorkbook.ActiveSheet
    $lastCol = .Cells(2, .Columns.Count).End($xlToLeft).Column; where '2' is the row ... 
EndWith
MsgBox("","",$LastCol)

I am not 100% sure I follow what you are trying to do but does that get you there?

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
1 hour ago, Jfish said:

The code I supplied was last used row in a col.  This is last used col in a row

;Find the last used column in a Row: row 1 in this example
Global Const $xlToLeft = -4159
With $oWorkbook.ActiveSheet
    $lastCol = .Cells(2, .Columns.Count).End($xlToLeft).Column; where '2' is the row ... 
EndWith
MsgBox("","",$LastCol)

I am not 100% sure I follow what you are trying to do but does that get you there?

Thank you again, I was able to understand and get that.
What I want is last used cell in a row...
I mean:
- In Row 1 last used cell is: F1(get data there)
- In Row 2 last used cell is: G1(get data there)
And get all this values from last used cell for in a row(row by row) of an entire Worksheet.

Regards
Alien.
 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Okay, now I think I understand ... how about this (selects cell in question):

;Find the very last used cell in a Row:
$rowNum=3; this is the row number you are targeting to find the last used cell
With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$rowNum).End($xlToLeft).Select
EndWith

This one returns the specific address:

;Find the very last used cell in a Row:
$rowNum=3; this is the row number you are targeting to find the last used cell
With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$rowNum).End($xlToLeft).Address
EndWith
MsgBox("","",$LastUsedCellInRow)

 

Edited by Jfish
1 person likes this

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

Hi again @Jfish
Thanks to your help I figure it out and end with this code:

#include<Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\Excel1.xlsx") ; your workbook here
Global Const $xlUp = -4162
Global Const $xlToLeft = -4159
;Number of Rows
With $oWorkbook.ActiveSheet
     $countRow = .Cells(.Rows.Count, "A").End($xlUp).row
EndWith
For $i = 1 To $countRow
    ; Number of Columns in 1 Row
    With $oWorkbook.ActiveSheet
        $countCol = .Cells($i, .Columns.Count).End($xlToLeft).Column
    EndWith
    $ColLetter = _Excel_ColumnToLetter($countCol)
    $aResult = _Excel_RangeRead($oWorkbook, Default,$ColLetter&$i , 1)
MsgBox(0,"",$aResult)
Next

And I re use your last code like this:

#include <Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\Excel1.xlsx") ; your workbook here
Global Const $xlUp = -4162
Global Const $xlToLeft = -4159
;Number of Rows
With $oWorkbook.ActiveSheet
     $countRow = .Cells(.Rows.Count, "A").End($xlUp).row
EndWith
For $i = 1 To $countRow
    ; Number of Columns in 1 Row
    With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$i).End($xlToLeft).Address
EndWith
MsgBox("","",$LastUsedCellInRow)
Next

But the return value is "$$column$$row" literally like this so that give me an extra $ sign, example:
"$C$3"(real value return) but it should be "C3"

Anyways I like this code more than the first one.

Thanks you so much.

Regards
Alien.

Share this post


Link to post
Share on other sites

This will remove the extra dollar signs ...

$rowNum=3; this is the row number you are targeting to find the last used cell
With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$rowNum).End($xlToLeft).Address
    $LastUsedCellInRow = StringReplace($LastUsedCellInRow,"$","")
EndWith
MsgBox("","",$LastUsedCellInRow)

 


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

@Jfish I have tried reading the help and after reading your continued conversation with Alien4u this is what I came up with.

Capture3.JPG.65557567e7bb5e65198d140cae6

The good news is I get no errors but the bad news is that it doesn’t work.

I am getting it to open the worksheet and copy but its only copies 0 from somewhere and paste 0 into notebook but the cell values have names. Any suggestions because the reading material isn’t that easy for me to understand. Also, I’m 100% sure I have the For loop wrong

Share this post


Link to post
Share on other sites

@Jfish & @alien4u 

(Please ignore post #16 Jfish i added more below)

 

I looked at your examples, read the help menu, and looked at other people's posts on similar issues to try and get a grasp of what is going on and this is what i was able to come up with. I get no errors when running this but i also get no results. 

Capture4.JPG.30c2b0d1efc10080d6b6ea85d28

 

I really appreciate both of your help.

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Could you please use the code tags <> when posting?  It makes it easier on our side.  That said $lastRow is only giving the a numeric value of the last row.  Therefore your range argument in _Excel_RangeRead is a single row number (when it should be a range). Try this instead:

$aResult = _Excel_RangeRead($oWorkbook,Default,"A1:A"&$lastRow)
_ArrayDisplay($aResult)

EDIT: I would also add some debugging.  When you are not sure where your errors are (this was just one - I did not study the whole thing) then best to test results as you step through the code with consolewrite or msgbox to see if you are getting the values you expect along the way.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

I believe I'm making major progress @Jfish but I have 1 last issue that i'm stumped on.

Global Const $xlUp = -4162
With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "A").End($xlUp).row
  EndWith
Local $aResult = _Excel_RangeRead($oWorkbook,Default,"A1:A"&$lastRow)
For $i = 0 to $lastRow
   _ArrayToClip($aResult,"", Default,0, $lastRow)

This code gets my array accurately (See below pic) but the last issue is when the loop restarts it keeps pasting the start of the _ArrayToClip which is "Test1"of the array, does the _ArrayToClip need to be its own loop inside of the $i loop? 

 571afbe77556a_ArrayDisplay.JPG.2b194967d  

Share this post


Link to post
Share on other sites

That is because you are starting your loop at 0 and "Test1" is in the 0 index of the array.  Try starting your for loop at 1 like this: for $i=1 to $lastrow.  Then the first value you will pass would be 2 in the example above.


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have an error:
      ==> The requested action with this object has failed.: $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count $iRowCount = .Range(^ ERROR  
      My code is:
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) Sleep(1000) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count Sleep(1000) _Excel_RangeWrite($oWorkbook_1, $oWorkbook_1.ActiveSheet,$aFileList[$i][2] , "AB3:AB"&$iRowCount) I have added some sleep because the application was crashing more often before, so i thought to slow down the code execution.
      But i didn't solve the issue.
      Has anyone an idea of what the problem might be?
      Thanks in advance.
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Written by GreenCan and water.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have a problem with the deletion of an empty row in Excel.
      My code:
       
      If $vRow_2 = "" Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf I want to delete the second row. $vRow_2 is an empty cell, "A2".
      After running the code, the second row is not deleted.
      I have tried also:
       
      If $vRow_2 = Null Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf But it doesn't work.
      Any suggestion?
      Thanks in advance.
    • 0Ethan0
      By 0Ethan0
      Ahoy Autoit Community!
      After many trials and errors I am unable to solve a problem I am facing and would appreciate any kind of input or better yet a solution 
      The Premise: An embeded slideshow viewer that runs after double-clicking an item in a ListView (each item will generate a different slideshow images).
      The Setup: GUI with a ListView Control and a simple exit button.
      The Issue: Once double clicked the slide plays however the GUI "locks"/non responsive until the slide is over. Same thing if I click on the "Test" button.
      The Culprit: I believe since it's in the images loop it can't accept any other commands until that loop is over.
      The Wish: I want to be able to use the GUI functions (selecting other items, clicking on button etc.) while the slideshow plays.
      The Code (stripped and simplified as much as I could):
      #include <GuiListView.au3> #include <File.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) HotKeySet("{Esc}", "_Exit") Global $c=0 Global $ssGDI[3], $ssGraphic[2], $ssImage Global Const $bg_color = "000000" Global Const $ssW = 480, $ssH = 320 Global $aFiles = _FileListToArrayRec("d:\testStage\", "*.jpg;*.png;*.bmp;*.gif;*.JPG;*.PNG;*.BMP;*.GIF", $FLTAR_FILES, $FLTAR_NORECUR ,$FLTAR_SORT ,$FLTAR_FULLPATH ) $guiW = 1200 $guiH = 726 $mainWindow = GUICreate("Slideshow Viewer", $guiW, $guiH, -1, -1, $WS_POPUP) $Button1 = GUICtrlCreateButton("Exit", 0, 0, 50, 50) GUICtrlSetOnEvent($Button1, "_Exit") $Button1 = GUICtrlCreateButton("Test", 60, 0, 50, 50) GUICtrlSetOnEvent($Button1, "Test") Global $ListView = GUICtrlCreateListView("Entry Name|Category", 5, 75, 195, 280) _GUICtrlListView_SetColumnWidth ($ListView, 0, 100) _GUICtrlListView_SetColumnWidth ($ListView, 1, 100) GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES) GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT) GUICtrlCreateListViewItem("Name 1|Category 1", $ListView) GUICtrlCreateListViewItem("Name 2|Category 2", $ListView) screenshotWidgetInit($ssW,$ssH, 690, 100) GUISetState(@SW_SHOW, $mainWindow) GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _Exit() EndSwitch WEnd Func Test() For $k = 1 To UBound($aFiles) - 1 screenshotWidgetTransition($aFiles[$k]) Next EndFunc Func ListView_Click() ConsoleWrite("Left Click") EndFunc Func ListView_DoubleClick() ConsoleWrite("Double Left Click") Test() EndFunc Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam) #forceref $hWndGUI, $MsgID, $wParam Local $tagNMHDR, $event, $hwndFrom, $code $tagNMHDR = DllStructCreate("int;int;int", $lParam) If @error Then Return $event = DllStructGetData($tagNMHDR, 3) Select Case $wParam = $ListView Select Case $event = $NM_CLICK ListView_Click () Case $event = $NM_DBLCLK ListView_DoubleClick () EndSelect EndSelect Return $GUI_RUNDEFMSG EndFunc Func screenshotWidgetTransition($image, $delay = 0, $speed = 1, $sleep = 2000) Local $a, $d = $c, $iX, $iY $ssImage = _GDIPlus_ImageLoadFromFile($image) $iX = _GDIPlus_ImageGetWidth($ssImage) $iY = _GDIPlus_ImageGetHeight($ssImage) $FDesktop=$ssH/$ssW $Fact =1 If $iX > $ssW And $FDesktop > ($iY/$iX) Then $Fact=$ssW/$iX ElseIf $iY > $ssH Then $Fact=$ssH/$iY EndIf $H1 = Round(($Fact * $iY),0) $W1 = Round(($Fact * $iX),0) _GDIPlus_GraphicsDrawImageRect($ssGraphic[$d], $ssImage,($ssW - $W1)/2, ($ssH - $H1) / 2,$W1,$H1) WinSetTrans($ssGDI[$d], "", 0) WinSetOnTop($ssGDI[$d], "", 1) For $a = 0 To 254 Step $speed WinSetTrans($ssGDI[$d], "", $a) Sleep($delay) Next WinSetTrans($ssGDI[$d], "", 254) WinSetOnTop($ssGDI[Not ($d)], "", 0) WinSetTrans($ssGDI[Not ($d)], "", 0) _GDIPlus_GraphicsClear($ssGraphic[Not ($d)]) $c = 1 - $d _GDIPlus_ImageDispose ($ssImage) ; very important to realease the pics Sleep($sleep) EndFunc ;==>screenshotWidgetTransition Func screenshotWidgetInit($ssW,$ssH,$ssX,$ssY) $ssGDI[2] = GUICreate("", $ssW, $ssH, $ssX, $ssY, $WS_POPUP, $WS_EX_MDICHILD, $mainWindow) $ssGDI[0] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2]) $ssGDI[1] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2]) ; GUISetBkColor("0x" & $bg_color, $ssGDI[2]) GUISetState(@SW_SHOW, $ssGDI[2]) GUISetState(@SW_SHOW, $ssGDI[0]) GUISetState(@SW_SHOW, $ssGDI[1]) WinSetTrans($ssGDI[0], "", 0) WinSetTrans($ssGDI[1], "", 0) _GDIPlus_Startup() $ssGraphic[0] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[0]) $ssGraphic[1] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[1]) _GDIPlus_GraphicsClear($ssGraphic[0], "0xFF" & $bg_color) _GDIPlus_GraphicsClear($ssGraphic[1], "0xFF" & $bg_color) EndFunc ;==>screenshotWidgetInit Func _Exit() _GDIPlus_ImageDispose($ssImage) _GDIPlus_GraphicsDispose($ssGraphic[0]) _GDIPlus_GraphicsDispose($ssGraphic[1]) GUIDelete($ssGDI[0]) GUIDelete($ssGDI[1]) GUIDelete($ssGDI[2]) _GDIPlus_Shutdown() Exit EndFunc ;==>_Exit I hope someone can shed light on this; perhaps a different approach is needed?
      Thank you in advance!
      P.S.
      The script is patched from different scripts of different users in the forum - thank you again users!
    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?