Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[functions] RotateTable


  • Please log in to reply
2 replies to this topic
Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
WARNING: This is old thread. It is continued here.

RotateTable is a collection of 3 functions which can rotate:
1) MS Excel table (selected range) - by Learning one
2) AHK object based table (2D array) - by Learning one
3) AHK string based table - by [VxE]
 
 
 
1) MS Excel table rotation
- see also: MS Excel transpose function: link1, link2
MSExcel_RotateTable%20diagram.jpg

F1::MSExcel_RotateTable()        ; Select range you want to rotate and press F1.
 
MSExcel_RotateTable() {	; Select range you want to rotate and call this function. By Learning one.
	try {
		;oExcel :=  Excel_Get()							; Recommended - by Jethrow/Sean http://www.autohotkey.com/board/topic/88337-ahk-failure-with-excel-get/#entry560328
		oExcel :=  ComObjActive("Excel.Application")	; Alternative - not always reliable...
		oRange :=  oExcel.Selection
	}
	catch
		return
	
	SafeArray := oRange.Value
	if (SafeArray.MaxIndex(1) = "")		; just one cell range - not safe array - nothing to do
		return
	
	;=== Get range dimensions, rotate range ===
	FirstRow := oRange.Row, FirstCoumn := oRange.Column
	TotalRows :=  oRange.Rows.Count, TotalColumns :=  oRange.Columns.Count	
	FirstCellAddress := oExcel.ActiveSheet.Cells(FirstRow, FirstCoumn).Address	; exa: $B$5
	StringReplace, FirstCellAddress, FirstCellAddress, $,, all					; exa: B5
	NewLastCellAddress := oExcel.ActiveSheet.Range(FirstCellAddress).Offset(TotalColumns-1,TotalRows-1).Address	; invert rows and columns
	StringReplace, NewLastCellAddress, NewLastCellAddress, $,, all
	oNewRange := oExcel.ActiveSheet.Range(FirstCellAddress ":" NewLastCellAddress)	; oNewRange is rotated oRange
	
	;=== Get values from SafeArray and build a new one ===
	NewSafeArray := ComObjArray(12, TotalColumns, TotalRows)	; invert rows and columns dimensions
	Loop % SafeArray.MaxIndex(1) ; loop through every row
	{
		RowNum := A_Index
		Loop % SafeArray.MaxIndex(2)  ; loop through every column
			NewSafeArray[A_Index-1, RowNum-1] := SafeArray[RowNum, A_Index]	; invert (note: zero based)
	}
	
	;=== Empty old range, populate and select new range ===
	oRange.Value := "", oNewRange.Value := NewSafeArray, oNewRange.Select
}
 
 
2) AHK object based table rotation
RotateTable(oTable) {	; Rotates table object. By Learning one.
	Out := [], TotalColumns := oTable.1.MaxIndex()
	Loop, % TotalColumns
		Out.Insert([])
	For k,v in oTable
	{
		For k2,v2 in v
			Out[k2][k] := v2 
	}
	return Out
}
 
/*
;Example:
oTable := [["Ana", "Tucker", "1988", "online"], ["Mia", "Meslin", "1991", "offline"]]
MsgBox % Table2String(oTable)
 
oRotatedTable := RotateTable(oTable)
MsgBox % Table2String(oRotatedTable)
 
;===Just a helper function ===
Table2String(oTable, RowsDelim = "`n", ColumnsDelim = "`t") {
	For k,v in oTable
	{
		For k2,v2 in v
			ThisRow .= ColumnsDelim v2
		ThisRow := SubStr(ThisRow, 1+StrLen(ColumnsDelim))
		Out .= RowsDelim ThisRow
		ThisRow := ""
	}
	return SubStr(Out, 1+StrLen(RowsDelim))
}
*/
 
3) AHK string based table rotation
- see this post by [VxE]

My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


peterm
  • Members
  • 60 posts
  • Last active: Jul 04 2013 05:24 PM
  • Joined: 25 Jul 2006

Interesting to see Excel being driven from outside.

Wouldn't one use the Transpose function in Excel?

 



Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

Links to transpose function added in first post. wink.png


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy