Jump to content

Excel Links Mapper

Recommended Posts


Here is another one from my archives that filled a specific need.

Here is the back story if you are interested.



I was getting constant calls from one department, complaining about how long it took (over 5 minutes at times) for their Excel files to open.

On investigating, I found that every month, they created a new file that linked back to the previous month's, as well as the previous year's files.  Often linking also to files that were missing (or renamed).

No matter how much I explained the need to break the links to old files that are no longer changing, they just didn't get it, and continued to complain that IT was not fixing the "network problems causing their files to open slow".

I finally broke down and wrote this script to create a visual map of their most recent file.  It took me almost a full day to print the massive graph and tape the sheets into a continuous scroll.  I then called a meeting with the supervisors, managers and the senior manager of that department.  As soon a the meeting started, I asked them all to step out into the hallway where, holding one end of the scroll, I threw the scroll down the hallway.  It didn't even unroll all the way, but the unrolled portion "thunked" against the far wall.  I then pointed at the map of this ONE Excel file and all of its links, and told them that this was their problem.  We didn't even walk back into the conference room.  Their senior manager looked at me and said, "I don't care what any one else here tells you.  Please go ahead and break ALL of the links.  If they need to reference the older data, they can copy and paste it."



Keep in mind that I wrote this script over 3 years ago, so it may not compile or run directly without some minor tweaks.  It also requires the use of GraphViz to build the graph.

#cs ----------------------------------------------------------------------------

    Project Name: ExcelLinksMapper
    Description: Analyse an Excel file's links and map them out.
    Creation Date: 9/26/2014
    AutoIt Version:
    Author: willichan
    Requires: Graphviz (http://graphviz.org/)

#ce ----------------------------------------------------------------------------

Opt("MustDeclareVars", 1)        ;0=no, 1=require pre-declare
Opt("TrayAutoPause",   0)        ;0=no pause, 1=Pause
Opt("TrayMenuMode",    0)        ;0=append, 1=no default menu, 2=no automatic check, 4=menuitemID  not return
Opt("TrayIconHide",    0)        ;0=show, 1=hide tray icon

Global Const $MyName=StringLeft(@ScriptName, StringInStr(@ScriptName,".", 0, -1)-1) ;get just the name portion of the script/exe name
Global Const $MyMutex=$MyName & "-82243BEBC30533A3" ;name the mutex for this app
Global $SQLloaded = False
Global $sDbName = @ScriptDir & "\db2gv.db"
ConsoleWrite($sDbName & @crlf)
If _MutexExists($MyMutex) Then Exit

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <file.au3>
#include <array.au3>
#include <excel.au3>


Func _ConfigInitialize()
    ;initializers here
    Global $sSQliteDll = _SQLite_Startup()
    If @error Then MsgBox(0, "SQLite Error", "could not load the DLL")
    Global $sSQLiteDB = _SQLite_Open($sDbName)
    If $sSQLiteDB = 0 Then MsgBox(0, "SQLite Error", "could not open the database")
    $SQLloaded =True
EndFunc  ;==>_ConfigInitialize

Func _ConfigDestroy()
    ;destructors here
    If $SQLloaded Then
EndFunc  ;==>_ConfigDestroy

Func _MutexExists($sOccurenceName)
    Local $ERROR_ALREADY_EXISTS = 183, $handle, $lastError
    $sOccurenceName = StringReplace($sOccurenceName, "\", "")
    $handle = DllCall("kernel32.dll", "int", "CreateMutex", "int", 0, "long", 1, "str", $sOccurenceName)
    $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    Return $lastError[0] = $ERROR_ALREADY_EXISTS
EndFunc  ;==>_MutexExists

Func __CreateTables()
    _SQLite_Exec($sSQLiteDB, "DROP TABLE IF EXISTS nodes;")
    _SQLite_Exec($sSQLiteDB, "CREATE TABLE IF NOT EXISTS nodes( name TEXT PRIMARY KEY, fileexists INTEGER);")
    _SQLite_Exec($sSQLiteDB, "DROP TABLE IF EXISTS links;")
    _SQLite_Exec($sSQLiteDB, "CREATE TABLE IF NOT EXISTS links( id INTEGER PRIMARY KEY, name1 TEXT, name2 TEXT, weight INTEGER);")

Func _Main()
    Local $sInfile, $vResult, $iErrLoop

    $sInfile = FileOpenDialog("Source File", @WorkingDir, "Excel files (*.xl*)", 1 + 2)
    If Not FileExists($sInfile) Then
        MsgBox(0, "Excel Links Mapper Error", "Unable to locate source file")

    $vResult = $SQLITE_IOERR
    $iErrLoop = 5
    While $vResult = $SQLITE_IOERR
        $vResult = _SQLite_Exec($sSQLiteDB, "INSERT OR IGNORE INTO nodes ('name', 'fileexists') VALUES (" & _SQLite_FastEscape($sInfile) & ", 1);")
        If Not $vResult = $SQLITE_OK Then Sleep(100)
        $iErrLoop -= 1
        If $iErrLoop = 0 Then
            ConsoleWrite($iErrLoop & " tries" & @CRLF & $sInfile & @CRLF)
            $vResult = $SQLITE_OK

    Global $hOutfile = FileOpen(@ScriptDir & "\" & $MyName & ".gv", 2)
    If $hOutfile = -1 Then
        MsgBox(0, $MyName & " ERROR", "Unable to upen file for output")
    ShellExecute(@ScriptDir & '\ExcelLinksMapper.png')
EndFunc   ;==>_Main

Func _GetExcelLinks($strFileName)
    Local $hQuery, $aCount, $iErrLoop, $vResult
    ConsoleWrite($strFileName & @CRLF)
    Local $iLoop, $iExists
    Local $aLinks
    Local Const $xlExcelLinks = 1
    Local $oExcel = _Excel_Open()

    Local $ret = _Excel_BookOpen_NoUpdate($oExcel, $strFileName, True, True)
    Local $err = @error
    If $err Then
        If Not IsObj($oExcel) Then
            ConsoleWrite($ret & " - " & $err & @CRLF)
    $aLinks = $oExcel.ActiveWorkbook.LinkSources($xlExcelLinks)
    _Excel_BookClose($oExcel, False)
    _Excel_Close($oExcel, False, True)
    If IsArray($aLinks) Then
        If UBound($aLinks) > 0 Then
            For $iLoop = 0 To UBound($aLinks) - 1
                If $aLinks[$iLoop] <> $strFileName Then
                    $iExists = FileExists($aLinks[$iLoop])
                    ConsoleWrite("DEBUG - Calling WriteNode()")
                    __WriteNode($aLinks[$iLoop], $iExists)
                    ConsoleWrite("DEBUG - Calling WriteLink()")
                    __WriteLink($strFileName, $aLinks[$iLoop])
                    If $iExists And ($aLinks[$iLoop] <> $strFileName) Then _GetExcelLinks($aLinks[$iLoop])
EndFunc   ;==>_GetExcelLinks

Func __WriteNode($sName, $iExists)
    Local $iErrLoop = 5 ;Number of attempts to make
    Local $vResult
        ConsoleWrite("DEBUG - WriteNode()" & @CRLF & "    _SQLite_Exec(INSERT OR IGNORE INTO nodes ('name', 'fileexists') VALUES (" & _SQLite_FastEscape($sName) & ", " & $iExists & ");) - create node entry" & @CRLF)
        $vResult = _SQLite_Exec($sSQLiteDB, "INSERT OR IGNORE INTO nodes ('name', 'fileexists') VALUES (" & _SQLite_FastEscape($sName) & ", " & $iExists & ");")
        If Not $vResult = $SQLITE_OK Then Sleep(100)
        $iErrLoop -= 1
        If $iErrLoop = 0 Then $vResult = $SQLITE_OK ;Used up all our attempts, so simulate a success
    Until $vResult = $SQLITE_OK
EndFunc   ;==>__WriteNode

Func __WriteLink($sName1, $sName2)
    Local $iErrLoop = 5 ;Number of attempts to make
    Local $vResult, $hQuery, $vCount
        ConsoleWrite("DEBUG - WriteNode()" & @CRLF & "    _SQLite_Query(SELECT weight FROM links WHERE 'name1'=" & _SQLite_FastEscape($sName1) & " AND 'name2'=" & _SQLite_FastEscape($sName2) & ";) - lookup link entry" & @CRLF)
    _SQLite_Query($sSQLiteDB, "SELECT weight FROM links WHERE 'name1'=" & _SQLite_FastEscape($sName1) & " AND 'name2'=" & _SQLite_FastEscape($sName2) & ";", $hQuery)
      ConsoleWrite("DEBUG - _SQLite_FetchData()" & @CRLF)
    _SQLite_FetchData($hQuery, $vCount)
    If UBound($vCount) > 1 Then _ArrayDisplay($vCount)
    If $SQLITE_OK And UBound($vCount) > 1 Then
        $vCount = $vCount[1] + 1
        $vCount = 1
        If $vCount = 1 Then
            ConsoleWrite("DEBUG - _SQLite_Exec() - create link entry" & @CRLF)
            $vResult = _SQLite_Exec($sSQLiteDB, "INSERT INTO links ('name1', 'name2', 'weight') VALUES (" & _SQLite_FastEscape($sName1) & ", " & _SQLite_FastEscape($sName2) & ", " & $vCount & ");")
            ConsoleWrite("DEBUG - _SQLite_Exec() - update link entry" & @CRLF)
            $vResult = _SQLite_Exec($sSQLiteDB, "UPDATE links SET 'weight'=" & $vCount & " WHERE 'name1'=" & _SQLite_FastEscape($sName1) & " AND 'name2=" & _SQLite_FastEscape($sName2) & ";")
        If Not $vResult = $SQLITE_OK Then Sleep(100)
        $iErrLoop -= 1
        If $iErrLoop = 0 Then $vResult = $SQLITE_OK ;Used up all our attempts, so simulate a success
    Until $vResult = $SQLITE_OK
EndFunc   ;==>__WriteLink

; #FUNCTION# ====================================================================================================================
; Author ........: SEO <locodarwin at yahoo dot com>
; Modified.......: litlmike, water, GMK, willichan
; ===============================================================================================================================
Func _Excel_BookOpen_NoUpdate($oExcel, $sFilePath, $bReadOnly = Default, $bVisible = Default, $sPassword = Default, $sWritePassword = Default)
    If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, @error, 0)
    If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
    If $bReadOnly = Default Then $bReadOnly = False
    If $bVisible = Default Then $bVisible = True
    ;; changing the second parameter on the following line to a 0 tells Excel not to update any links.
    Local $oWorkbook = $oExcel.Workbooks.Open($sFilePath, 0, $bReadOnly, Default, $sPassword, $sWritePassword)
    If @error Then Return SetError(3, @error, 0)
    $oExcel.Windows($oWorkbook.Name).Visible = $bVisible
    ; If a read-write workbook was opened read-only then return an error
    If $bReadOnly = False And $oWorkbook.Readonly = True Then Return SetError(4, 0, $oWorkbook)
    Return $oWorkbook
EndFunc   ;==>_Excel_BookOpen_NoUpdate

Func _GenerateGraph()
    RunWait(@ScriptDir & '\GraphViz238\bin\dot.exe -Tpng "' & @ScriptDir & '\' & $MyName & '.gv" -o "' & @ScriptDir & '\ExcelLinksMapper.png"')
EndFunc   ;==>_GenerateGraph

Func _WriteHeader()
    __OutLine(0, "digraph main {")
EndFunc   ;==>_WriteHeader

Func _WriteNodes()
    Local $aResult, $iRows, $iColumns, $iRval
    Local $iLoop, $sStyle
    __OutLine(1, "// Nodes")
    $iRval = _SQLite_GetTable($sSQLiteDB, "SELECT name, fileexists FROM nodes;", $aResult, $iRows, $iColumns)
    If @error Then ConsoleWrite("_WriteNodes()" & @CRLF & "_SQLite_GetTable")
    If $iRval = $SQLITE_OK Then
        For $iLoop = 1 To $iRows
            If $aResult[($iLoop * 2) + 2] > 0 Then
                $sStyle = "normal"
                $sStyle = "missing"
            __OutNode($aResult[1 + (2 * $iLoop)], $sStyle)
        MsgBox($MB_SYSTEMMODAL, "SQLite Error: " & $iRval, _SQLite_ErrMsg() & @CRLF & "in _WriteNotes() calling _SQLite_GetTable()")
EndFunc   ;==>_WriteNodes

Func __OutNode($sName, $sStyle = Default)
    If $sStyle = Default Then $sStyle = "Normal"
    Switch StringLower($sStyle)
        Case "missing"
            __OutLine(1, __MakeName($sName) & ' [label="' & StringReplace($sName, "\", "\\") & '",color=red,fontcolor=red,shape=octagon];')
        Case Else ;"normal", Default
            __OutLine(1, __MakeName($sName) & ' [label="' & StringReplace($sName, "\", "\\") & '",color=black,fontcolor=black,shape=box];')
EndFunc   ;==>__OutNode

Func _WriteLinks()
    Local $aResult, $iRows, $iColumns, $iRval
    Local $iLoop, $sStyle, $aCount
    __OutLine(1, "// Links")
    $iRval = _SQLite_GetTable($sSQLiteDB, "SELECT name1, name2 FROM links ORDER BY name1 ASC, name2 ASC;", $aResult, $iRows, $iColumns)
    If $iRval = $SQLITE_OK Then
        For $iLoop = 1 To $iRows
            __OutLink($aResult[1 + (2 * $iLoop)], $aResult[2 + (2 * $iLoop)])
        MsgBox($MB_SYSTEMMODAL, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndFunc   ;==>_WriteLinks

Func __OutLink($sName1, $sName2, $iWeight = 1)
    __OutLine(1, __MakeName($sName1) & ' -> ' & __MakeName($sName2) & ';')
EndFunc   ;==>__OutLink

Func _WriteFooter()
    __OutLine(0, "}")
EndFunc   ;==>_WriteFooter

Func __OutLine($iTabs, $sText)
    Local $iLoop
    If $iTabs > 0 Then
        For $iLoop = 1 To $iTabs
            FileWrite($hOutfile, "  ")
            ;ConsoleWrite("  ")
    FileWriteLine($hOutfile, $sText)
EndFunc   ;==>__OutLine

Func __MakeName($sText)
    Local $sNewName = StringReplace($sText, "\", " ")
    $sNewName = StringReplace($sNewName, "/", " ")
    $sNewName = StringReplace($sNewName, "'", " ")
    $sNewName = StringReplace($sNewName, '"', " ")
    $sNewName = StringReplace($sNewName, ':', " ")
    $sNewName = StringReplace($sNewName, '.', " ")
    $sNewName = StringReplace($sNewName, '-', " ")
    $sNewName = StringReplace($sNewName, '$', " ")
    $sNewName = StringStripWS($sNewName, 8)
    Return StringLower($sNewName)
EndFunc   ;==>__MakeName


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

    • MrCheese
      By MrCheese
      Hi guys,
      without including everything (unless you want it)
      I am copying data from a table in chrome and wanting to paste it into excel.
      Copying in Chrome works.
      I can paste it into the field i want by emulating goto -> ctrl V:
      WinActivate($dataload) WinWaitActive($dataload) Sleep(500) $oWorkbook1.Sheets("ItemReturn").Activate Sleep(500) $msg = "Measuring Sheet" conwrite() ttips2() Local Const $xlUp = -4162 With $oWorkbook1.ActiveSheet ; process active sheet $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) ; get a Range that contains the last used cells $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, "B")).Rows.Count ; get the the row count for the range starting in row/column 1 and ending at the last used row/column $iLastCell = .Cells($iRowCount + 1, "B").End($xlUp).Row ; start in the row following the last used row and move up to the first used cell in column "B" and grab this row number EndWith $NewStartCell = $iLastCell + 2 $msg = "moving to location" conwrite() ttips2() Sleep(250) Send("^g") WinWait("Go To") Sleep(100) Send("B" & $NewStartCell) Sleep(100) Send("{ENTER}") Sleep(500) Send("^v")  
      But, I want to use _excel_rangecopypaste, pasting from the clipboard
      _Excel_RangeCopyPaste($oWorkbook1.ActiveSheet, default, "B" & $NewStartCell,default,$xlPasteValuesAndNumberFormats) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeCopy Example 2", "Error pasting cells." & @CRLF & "@error = " & @error & ", @extended = " & @extended) however, this gives me error 4 , extended@:  -2147352567
      How can i fix this or find out how to debug this error?
    • Simpel
      By Simpel
      I try to figure out who is using a excel workbook which I can only open "read only". I use this code:
      #include <Array.au3> #include <Excel.au3> Local $sFile = ; excel file with path on a network drive Local $oExcel = _Excel_Open(True, True) Local $oTabelle = _Excel_BookOpen($oExcel, $sFile) Local $aUsers If IsObj($oTabelle) Then $aUsers = $oTabelle.UserStatus _ArrayDisplay($aUsers) EndIf If I am the one allowed to write to the excel file (I'm the first one who opened it) then I will get an array with myself:

      If my collegue opened the excel file first and I run the code I get the following error message:
      "H:\_Conrad lokal\Downloads\AutoIt3\_COX\Tests\test.au3" (9) : ==> The requested action with this object has failed.: $aUsers = $oTabelle.UserStatus $aUsers = $oTabelle^ ERROR The excel file is on a network drive. Is that's the problem?
      Regards, Conrad
    • robertocm
      By robertocm
      change linked image paths in excel 2007 Open XML Files with AutoIt and 7-zip:
      #include <File.au3> ;Change this Local $sFind = "C:\Users\MyUserName\Documents\MyImageFolder\My%20Image1.png" Local $sReplace = "C:\Users\ANOTHERUSERNAME\Documents\AnotherImageFolder\My%20Image1.png" Local Const $sMessage = "Directory to change excel image paths" Local $sFileSelectFolder = FileSelectFolder($sMessage, "") Local $sTempDir = @ScriptDir & "\testdir" ;Required 7-zip Local $PathZipProgram = @ProgramFilesDir & "\7-Zip\" If Not(FileExists($PathZipProgram & "\7z.exe")) Then MsgBox(16, "", "7z.exe not found in path " & $PathZipProgram) Exit EndIf ;look for excel files in selected directory and all subdirectories Local $SFileList = _FileListToArrayRec($sFileSelectFolder, "*.xls.;*.xlsm", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) If Not @error Then For $i = 1 To $SFileList[0] DirRemove($sTempDir, 1) ;use x command to keep the folder stucture, -aoa Overwrite All existing files without prompt, use -r to unzip the subfolders from the zip file RunWait('"' & $PathZipProgram & '7z.exe" x -aoa -r "' & $SFileList[$i] & '" -o"' & $sTempDir & '" -y', $PathZipProgram, @SW_HIDE) __ReplaceImagePaths($sTempDir, $sFind, $sReplace) RunWait('"' & $PathZipProgram & '7z.exe" a -r "' & $SFileList[$i] & '" "' & $sTempDir & '\*" -tzip -y', $PathZipProgram, @SW_HIDE) Next Else MsgBox(16, "Error", "No files were found in the folder specified.") EndIf DirRemove($sTempDir, 1) Func __ReplaceImagePaths($sTempDir, $sFind, $sReplace) ;List all files with .xml.rels extension in the directory \xl\drawings\_rels Local $aFileList = _FileListToArray($sTempDir & "\xl\drawings\_rels", "*.xml.rels", 1, True) If @error = 1 Then ;MsgBox (0, "", "Path was invalid") SplashTextOn("Title", "Path was invalid", -1, -1, -1, -1, 1, "", 24) Sleep(2000) SplashOff() Exit EndIf If @error = 4 Then ;MsgBox (0, "No files", "No files were found") SplashTextOn("Title", "No files were found", -1, -1, -1, -1, 1, "", 24) Sleep(2000) SplashOff() Exit EndIf Local $iRetval ;Loop through the array For $i = 1 To $aFileList[0] $iRetval = _ReplaceStringInFile($aFileList[$i], $sFind, $sReplace) Next EndFunc  
      Some references:
      https://stackoverflow.com/questions/37145369/change-path-to-picture-links-in-excel http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp EDITED:
      Note: it seems that if User Account Control (UAC) is enabled then 7zip is unable to overwrite the destination file (using the same name).
      In this case, a possible solution would be to rename the original excel file before (see _PathSplit in help file).
      In my case i prefer just to disable UAC
    • SOF-TECH
      By SOF-TECH
      Dear all,
      Can someone show  me how to en hance the below function to write in CSV  into column  and rows the input values ? 
      I am getting this result: 

      I would like the result to be as this 

      From A1:C1 is for headers
      From A2:C2 is for input Data
      Global Const $GUI_EVENT_CLOSE = -3 $sDataFilePath = @ScriptDir & "\Records.csv" #region ### START Koda GUI section ### Form= $Form1 = GUICreate("Demo1: New Record", 580, 115) $Input1 = GUICtrlCreateInput("", 10, 30, 270, 21) $Input2 = GUICtrlCreateInput("", 300, 30, 270, 21) $Input3 = GUICtrlCreateInput("", 10, 80, 270, 21) $Label1 = GUICtrlCreateLabel("Name:", 10, 10, 35, 17) $Label2 = GUICtrlCreateLabel("ID:", 300, 10, 18, 17) $Label3 = GUICtrlCreateLabel("Phone No:", 10, 60, 55, 17) $Button1 = GUICtrlCreateButton("Save to CSV", 450, 70, 120, 30) GUISetState(@SW_SHOW) #endregion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _ExportData() MsgBox(64, @ScriptName, "Record Saved.") EndSwitch WEnd Func _ExportData() If Not FileExists($sDataFilePath) Then FileWriteLine($sDataFilePath, "Name;ID;Phone No.;") EndIf For $i = $Input1 To $Input3 FileWrite($sDataFilePath, GUICtrlRead($i) & ";") Next FileWriteLine($sDataFilePath, "") EndFunc ;==>_ExportData May be Excel UDF has be to be added but I can manage that my self  
      Thank you in advance
    • MrCheese
      By MrCheese
      HI there
      this is driving me nuts - i get the row count, but not the column count - what am I missing? Thanks for your help!
      #include <Excel.au3> #include <Array.au3> #include <MsgBoxConstants.au3> ; XlDirection enumeration: https://msdn.microsoft.com/en-us/library/office/ff820880.aspxGlobal $oExcel = _Excel_Open() Global $xlup = -4162 Global $xlByRows, $xlPrevious, $xlByColumns Global $oExcel = _Excel_Open() $bookname = "temp.xlsx" $sWorkbook = @ScriptDir & "\" & $bookname Global $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook, Default, Default, True) With $oWorkbook.ActiveSheet ; process active sheet $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) ; get a Range that contains the last used cells $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count ; get the the row count for the range starting in row/column 1 and ending at the last used row/column $iColCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Column.Count MsgBox(0, "", "row:" & $iRowCount & "Col:" & $iColCount) EndWith  
      for context - i want to :
      * count columns used in excel
      * create ini file from the rows in each column - finishing at the last column used - i.e. one column for one ini file; containing 15 rows or so.
      is it better to read the entire sheet to an array via the sheettoarray function? then read it from that?

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.