Sử dụng Calc2latex như thế nào?

Việc lập một bảng trên MS Excel hoặc trên LibreOffice Calc rồi xuất ra LaTeX là việc dễ thực hiện nhất để lập một cái bảng phức tạp.

Trong bài viết này chúng tôi sẽ hướng dẫn các bạn:

  1. Cài đặt LibreOffice 3.6 lên máy tính.
  2. Download calc2latex
  3. Cấu hình LibreOffice Calc để sử dụng được calc2latex
  4. Ví dụ

Để download LibreOffice các bạn nên tìm đến link sau đây:
http://www.libreoffice.org/download/?type=win-x86&lang=en-US&version=3.6.4

Download file msi (205MB) về cài đặt lên máy tính như bất cứ một phần mềm Windows nào. Đây là một bộ văn phòng ấn tượng, miễn phí và tương thích khá cao với MS Office, ví dụ có thể đọc các công thức toán học viết bằng Mathtype, chạy được các script viết bằng VBA. Hơn nữa với các plug-in(s) writer2latex, các bạn xuất file doc ra file LaTeX, plug-in TeXMath Equations các bạn có thể soạn các công thức toán học bằng LaTeX và insert vào file doc như thường làm với MathType bên Windows.

Sau khi cài đặt xong LibreOffice, các bạn sẽ có 4 chương trình (thường gọi là văn phòng tứ bảo): LibreOffice Writer (y chang như MS Word), LibreOffice Calc (y chang như MS Excel), LibreOffice Impress (y chang như MS Powerpoint) và LibreOffice Data (như MS Access).

Các bạn download calc2latex tại đây.

Giải nén file zip thành một thư mục đặt tại đâu cũng được, tạm gọi là thư mục calc2latex_024_eur_latex. Các bạn sẽ thấy nhiếu file nhưng chỉ dụng 2 file là Calc2LaTeX.xba và script.xlb

Khởi dộng LibreOffice Calc. Tìm đến Tools, Macros, Organize Macros, LibreOffice Basic…

Click vào “Organizer”

Chọn tab “Libraries” rồi click “Import”

Duyệt tới thư mục calc2latex_024_eur_latex ở trên và chọn script.xlb, trở vào màn hình trước đó của LibreOffice Calc các bạn check vào Calc2latex nhấn OK và Close hai lần. Thế là xong.

Sử dụng:

Giả sử các bạn có một bảng tính trong MS Excel. Dùng LibreOffice Calc mở file xls này vào Libreoffice Calc.

Quét bảng tính

Chọn Tools, Macros, Run Macro,

Tìm trong My Macros -> “Calc2LaTeX” -> “Calc2LaTeX”. Trên panel của cửa sổ bên phải, chọn “Main” và click vào “Run”.

Ngay lập tức cái bảng trong vùng bị quét sẽ được copy thành code, các bạn dán vào LaTeX để sử dụng.

a

7 responses to “Sử dụng Calc2latex như thế nào?

  1. Trương Quốc Đại

    Thưa Thầy em tạo cái bảng như Thầy hướng dẫn, mọi việc diễn ra bình thường nhưng khi sử dụng, chọn
    run macro lại thấy thông báo: “LibreOffice required a Java runtime environment (JRE) to perform this task, please install a JRE and restart LibreOffice”
    sau đó nếu chọn OK thì mọi việc bình thường, đến khi chọn main -> run thì có thông báo:
    ” basic runtime error
    an exception occurred
    type: com.sun.star.container.NoSuchElementException Message: .” và vẫn có đoạn code nhưng không copy được, xin Thầy chỉ giúp em!

    • Vấn đề cài JRE theo yêu cầu không thực hiện cũng được.

      Nếu em thấy đã xuất ra đoạn code thì nó đã tự dộng quét hết đoạn code (tương đương với Bấm Ctrl A), em phải bấm Ctrl C, sau đó vào TeXMaker, bấm Ctrl V để dán.

      Nếu em không dán được (nghĩa là không copy được) thì em phải cài JRE. Em lưu ý LibreOffice viết bằng Java, nên em nên nếu cần thiết em bắt buộc phải cài JRE. Còn nếu dán được thì cứ thế mà sử dụng.

      Thầy chạy Calc2latex trến máy ảo Win7, vẫn thấy có thông báo cài JRE nhưng khi chạy macro vẫn xuất ra code bình thường, copy bình thường và dán bình thường.

      Khi thử hết mọi cách mà không thực hiện được em cần phải quan tâm tới máy tính WINDOWS nhiều hơn nữa, nó có bị nhiễm virus hay không v.v…

  2. Trương Quốc Đại

    Thưa Thầy em dán vào được rồi nhưng báo lỗi, Thầy coi dùm em đoạn này:
    \documentclass[12pt,a4paper]{book}
    \usepackage[utf8]{inputenc}
    \usepackage[vietnam]{babel}
    \usepackage{utopia}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \begin{document}
    REM ***** BASIC *****

    Option Explicit

    REM ***** USER SETTINGS ******
    ‘ Set the character encoding that should be used.
    ‘ The character encoding names refer to the document http://www.iana.org/assignments/character-sets.
    ‘ Which character sets are supported depends on the implementation.
    ‘ For example, you can choose a charset from”ASCII”, “Latin-1”, “ISO-8859-1”, “Latin-9”, “ISO-8859-15”,
    ‘ “windows-1252″,”UTF-8”, “SJIS”, “EUC-JP”, and so on.
    public const user_charset = “windows-1252”

    ‘ Set the line feed character.
    ‘ You can choose from “lf”(for Linux), “crlf”(for Windows), and “cr”.
    public const user_linefeed = “crlf”
    REM ***** END ****************

    ‘ to save settings used by the “save as” file dialog
    Global dialogDirectory as string
    Global dialogFilename as string

    ‘ to save settings used by the GUI setting dialog
    ‘Global aGlobalSetting as GlobalSettings
    Global aGlobalSetting_tabularEnable as Integer
    Global aGlobalSetting_labelString as String
    Global aGlobalSetting_captionString as String
    Global aGlobalSetting_captionPosition as Integer
    Global aGlobalSetting_horizontalPosition as Integer
    Global aGlobalSetting_insertPosition as Integer
    Global aGlobalSetting_ruledLine as Integer
    Global aGlobalSetting_formatCells as Integer
    Global aGlobalSetting_outputResult as Integer
    Global aGlobalSetting_replaceReservedWord as Integer

    public const strAppname = “Calc2LaTeX”
    public const strVersion = “0.2.4”

    public strEOL As String

    REM Reserved Word of TeX
    public const strBeginTabular = “\begin{tabular}”
    public const strEndTabular = “\end{tabular}”
    public const strBeginLeft = “\begin{flushleft}”
    public const strEndLeft = “\end{flushleft}”
    public const strBeginCenter = “\begin{center}”
    public const strEndCenter = “\end{center}”
    public const strBeginRight = “\begin{flushright}”
    public const strEndRight = “\end{flushright}”

    public const strBeginTable = “\begin{table}”
    public const strEndTable = “\end{table}”

    public const strHeadLabel = “\label{”
    public const strTailLabel = “}”
    public const strHeadCaption = “\caption{”
    public const strTailCaption = “}”

    public const strHeadTabularOption = “{”
    public const strTailTabularOption = “}”

    public const strTabularOptionLeft = “l”
    public const strTabularOptionCenter = “c”
    public const strTabularOptionRight = “r”

    public const strTableHere = “[htbp]”
    public const strTableTop = “[tbp]”
    public const strTableBottom = “[bp]”
    public const strTableUseOnePage = “[p]”

    public const strHeadMulticolumn = “\multicolumn{”
    public const strSplitMulticolumn = “}{”
    public const strTailMulticolumn = “}”

    public const strHeadTableIrregularCellPosition = “\multicolumn{1}{”
    public const strSplitTableIrregularCellPosition = “}{”
    public const strTailTableIrregularCellPosition = “}”

    public const strHoriBorderLine = “\hline”
    public const strVertBorderLine =”|”

    public const strHeadCline = “\cline{”
    public const strSplitCline =”-”
    public const strTailCline = “}”

    public const strSplit = ” & ”
    public const strEndColumn = ” \\”

    ‘ for replacement of reserved words
    ‘public const strBackslash =”\”
    ‘public const strReplacedBackslash = “$\backslash$” ‘ you can’t use “\verb|\|” in multicolumn.
    public const strPercent =”%”
    public const strReplacedPercent =”\%”
    public const strOpenningBrace=”{”
    public const strReplacedOpenningBrace=”\{”
    public const strClosingBrace = “}”
    public const strReplacedClosingBrace = “\}”
    public const strUnderBar = “_”
    public const strReplacedUnderBar = “\_”
    public const strAmp = “&”
    public const strReplacedAmp = “\&”
    public const strSharp = “#”
    public const strReplacedSharp = “\#”
    public const strDollar = “$”
    public const strReplacedDollar = “\$”

    public const constON As Boolean = True
    public const constOFF As Boolean = False

    Type ReturnValueOfSettings
    strTableHead As String
    strTableTail As String
    bLineONOFF As Boolean
    bEnableFormattingCells As Boolean
    bResultWindow As Boolean
    bEnableReplaceWords As Boolean
    End Type

    Public oDocument As Object

    REM MAIN
    Sub Main
    ‘init
    ‘static aGlobalSetting as GlobalSettings

    Dim oSheet As Object
    Dim oSelection As Object
    Dim strTable As String

    ‘ ENTER
    ‘strEOL = Chr(10) + Chr(13)
    if user_linefeed = “crlf” then
    strEOL =Chr(13) + Chr(10)
    elseif user_linefeed = “lf” then
    strEOL = Chr(10)
    else
    strEOL = Chr(13)
    endif

    oDocument = ThisComponent

    oDocument.addActionLock

    Dim aReturnValueOfSettings as ReturnValueOfSettings
    aReturnValueOfSettings = Settings2()

    oSelection = oDocument.CurrentController.Selection

    strTable = makeTableBody(oSelection,aReturnvalueOfSettings.bLineONOFF, aReturnValueOfSettings.bEnableFormattingCells, _
    aReturnValueOfSettings.bEnableReplaceWords)
    strTable = strBeginTabular + strTable + strEndTabular + strEOL
    ‘ strTable = Settings(strTable)

    strTable = aReturnValueOfSettings.strTableHead + strTable + aReturnValueOfSettings.strTableTail

    ‘ UNDER DEVELOPMENT
    If aReturnValueOfSettings.bResultWindow = constON then
    ShowResults(strTable)
    Else
    outputResultWithFileDialog(strTable)
    End If

    oDocument.removeActionLock
    End Sub

    REM TABLE BODY
    Function MakeTableBody(oCellRange as Object, bONOFF as Boolean, bEnableFormattingCells as Boolean, bEnableReplaceWords as Boolean ) As String
    Dim nCols,nRows As Long
    Dim i,j As Long
    Dim oCell As Object
    Dim oBaseRangeAddress As New com.sun.star.table.CellRangeAddress
    nCols = oCellRange.Columns.Count
    nRows = oCellRange.Rows.Count
    oBaseRangeAddress = oCellRange.getRangeAddress

    Dim strTabularOption As String
    strTabularOption = MakeTabularOption(oCellRange)

    Dim strTable As String
    strTable=””
    strTable = strTable + strHeadTabularOption+ insertBorderMark(strTabularOption,bONOFF) + _
    strTailTabularOption + strEoL

    strTable= strTable + makeHLine(bONOFF) + strEoL

    Dim flagMargedHidden As Integer
    Dim oSheet As Object
    Dim oCursor As Object
    Dim oRangeAddress As New com.sun.star.table.CellRangeAddress

    Dim nClinePrev As Integer
    Dim strColumnLine As String

    oSheet = ThisComponent.CurrentController.ActiveSheet

    For j=0 To (nRows-1)
    nClinePrev = 1
    strColumnLine = “”
    For i=0 To (nCols-1)
    ‘ Marged Cell Check

    oCell = oCellRange.getCellByPosition(i,j)
    oCursor = oSheet.createCursor()
    oCursor.gotoStart()

    REM [DEV INFO] ver024 FIX BEGIN
    ‘ oCursor.gotoOffset(i+oBaseRangeAddress.StartColumn-1 ,j+oBaseRangeAddress.StartRow-1)
    Dim offsetBaseColumn as long, offsetBaseRow as long
    if oBaseRangeAddress.StartColumn = 0 then
    offsetBaseColumn = oBaseRangeAddress.StartColumn
    else
    offsetBaseColumn = oBaseRangeAddress.StartColumn -1
    endif
    if oBaseRangeAddress.StartRow = 0 then
    offsetBaseRow = oBaseRangeAddress.StartRow
    else
    offsetBaseRow = oBaseRangeAddress.StartRow -1
    endif
    oCursor.gotoOffset(i+offsetBaseColumn ,j+offsetBaseRow)
    REM [DEV INFO] ver024 FIX END

    oCursor.collapseToMergedArea()
    oRangeAddress = oCursor.getRangeAddress()

    Dim strACellOutput As String
    flagMargedHidden = 0

    ‘ horizontal check
    If (oRangeAddress.EndColumn oRangeAddress.StartColumn ) then
    ‘ Marged
    If (oRangeAddress.StartColumn – oBaseRangeAddress.StartColumn = i ) Then
    ‘ First Column
    strACellOutput = strHeadMulticolumn + str(oRangeAddress.EndColumn – oRangeAddress.StartColumn+1) + strSplitMulticolumn
    If i=0 then
    strACellOutput = strACellOutput + makeVLine(bONOFF) + convPositionToString(getPosition(oCell)) + makeVLine(bONOFF)
    else
    strACellOutput = strACellOutput + convPositionToString(getPosition(oCell)) +makeVLine(bONOFF)
    end if

    ‘strACellOutput = strACellOutput + strSplitMulticolumn + ReplaceReservedWords(oCell.String) +strTailMulticolumn
    strACellOutput = strACellOutput + strSplitMulticolumn + getFormattedCellString(ReplaceReservedWords(oCell.String, bEnableReplaceWords),oCell,bEnableFormattingCells) +strTailMulticolumn

    ‘ cline
    if((oRangeAddress.EndRow -oBaseRangeAddress.StartRow) j) And _
    (nClinePrev <=(i+1-1)) then
    'strColumnLine = strColumnLine + strHeadCline + Str(nClinePrev) + "-" + Str(i+1-1) + strTailCline
    strColumnLine = strColumnLine + makeCLine(nClinePrev, i+1-1, bONOFF)
    nClinePrev = i+2
    endif
    else
    ' Hidden Cells (Marged)
    flagMargedHidden = 1
    strACellOutput=""
    ' cline
    if((oRangeAddress.EndRow-oBaseRangeAddress.StartRow) j) then
    nClinePrev = i+2
    endif
    endif

    ‘ vertical check
    elseif oRangeAddress.EndRow oRangeAddress.StartRow then
    ‘ marged
    strACellOutput =strHeadMulticolumn+ str(oRangeAddress.EndColumn – oRangeAddress.StartColumn+1) + strSplitMulticolumn
    If i=0 then
    strACellOutput = strACellOutput + makeVLine(bONOFF) + convPositionToString(getPosition(oCell)) + makeVLine(bONOFF)
    else
    strACellOutput = strACellOutput + convPositionToString(getPosition(oCell)) +makeVLine(bONOFF)
    end if

    ‘strACellOutput = strACellOutput + strSplitMulticolumn + ReplaceReservedWords(oCell.String) +strTailMulticolumn
    ‘ hide cells which is covered with a marged cell
    strACellOutput = strACellOutput + strSplitMulticolumn
    if oRangeAddress.StartRow-oBaseRangeAddress.StartRow = j then
    ‘strACellOutput = strACellOutput + ReplaceReservedWords(oCell.String)
    strACellOutput = strACellOutput + getFormattedCellString(ReplaceReservedWords(oCell.String, bEnableReplaceWords),oCell,bEnableFormattingCells)

    endif
    strACellOutput = strACellOutput + strTailMulticolumn

    ‘ cline
    if((oRangeAddress.EndRow -oBaseRangeAddress.StartRow) j) And _
    (nClinePrev <=(i+1-1)) then
    'strColumnLine = strColumnLine + strHeadCline + Str(nClinePrev) +strSplitCline + Str(i+1-1) + strTailCline
    strColumnLine = strColumnLine + makeCLine(nClinePrev,i+1-1, bONOFF)
    endif
    if((oRangeAddress.EndRow -oBaseRangeAddress.StartRow) j) then
    nClinePrev = i+2
    endif

    else
    ‘ not Marged
    If StrComp(Mid(strTabularOption,i+1,1), _
    convPositionToString(getPosition(oCell)))=0 Then
    ‘ strACellOutput = ReplaceReservedWords(oCell.String)
    strACellOutput = getFormattedCellString(ReplaceReservedWords(oCell.String, bEnableReplaceWords),oCell,bEnableFormattingCells)

    else
    strACellOutput = strHeadTableIrregularCellPosition
    If i=0 Then
    strACellOutput = strACellOutput + makeVLine(bONOFF)
    End If
    ‘ strACellOutput = strACellOutput + _
    ‘ convPositionToString(getPosition(oCell)) + _
    ‘ makeVLine(bONOFF) + _
    ‘ strSplitTableIrregularCellPosition + _
    ‘ ReplaceReservedWords(oCell.String)+strTailTableIrregularCellPosition

    strACellOutput = strACellOutput + _
    convPositionToString(getPosition(oCell)) + _
    makeVLine(bONOFF) + _
    strSplitTableIrregularCellPosition + _
    getFormattedCellString(ReplaceReservedWords(oCell.String, bEnableReplaceWords),oCell,bEnableFormattingCells) +strTailTableIrregularCellPosition

    endif
    endif
    oCursor = Nothing

    strTable = strTable + strACellOutput

    If (i (nCols-1)) AND (flagMargedHidden 1) AND (oRangeAddress.EndColumn (oBaseRangeAddress.StartColumn + nCols-1)) Then
    strTable = strTable + strSplit
    End If
    Next i

    if(nClinePrev =1) then
    ‘ this line is not marged
    strTable = strTable + strEndColumn + ” ” + makeHLine(bONOFF) + strEOL
    else
    If nClinePrev = nCenter) And (nLeft >= nRight) Then
    ‘detectMaxLCR = com.sun.star.table.CellHoriJustify.LEFT
    detectMaxLCR = strTabularOptionLeft
    Elseif (nCenter >= nLeft) And (nCenter>=nRight) Then
    ‘detectMaxLCR = com.sun.star.table.CellHoriJustify.CENTER
    detectMaxLCR = strTabularOptionCenter
    Else
    ‘detectMaxLCR = com.sun.star.table.CellHoriJustify.RIGHT
    detectMaxLCR = strTabularOptionRight
    End If
    End Function

    ‘ex)
    ‘strItem=”%a%bcde%f%g%”
    ‘strSign = “%”
    ‘strReplacedSign = “\%”
    ‘return = “\%a\%bcde\%f\%g\%”
    Sub ReplaceString(strItem As String,strSign As String,strReplacedSign As String) As String
    Dim nSeek As Integer
    Dim nItemLength As Integer

    nSeek=InStr(1,strItem,strSign)
    While(nSeek 0)
    nItemLength = Len(strItem)
    strItem = Left(strItem,nSeek-1)+ strReplacedSign + Right(strItem,nItemLength-nSeek)
    nSeek=nSeek+Len(strReplacedSign)
    nSeek=InStr(nSeek,strItem,strSign)
    Wend

    ReplaceString=strItem
    End Sub

    ‘FIX !
    Sub ReplaceReservedWords (Byval strItem As String, Byval bEnable as Boolean) As String
    Dim reservedWords as variant
    Dim replacedWords as variant

    ‘ reservedWords = Array(strBackslash,strPercent,strOpenningBrace,strClosingBrace,strUnderBar,strAmp,strSharp,strDollar)
    ‘ replacedWords = Array(strReplacedBackslash,strReplacedPercent,strReplacedOpenningBrace,strReplacedClosingBrace, _
    ‘ strReplacedUnderBar,strReplacedAmp,strReplacedSharp,strReplacedDollar)
    reservedWords = Array(strPercent,strOpenningBrace,strClosingBrace,strUnderBar,strAmp,strSharp,strDollar)
    replacedWords = Array(strReplacedPercent,strReplacedOpenningBrace,strReplacedClosingBrace, _
    strReplacedUnderBar,strReplacedAmp,strReplacedSharp,strReplacedDollar)

    if (bEnable = constON) then
    Dim i as Integer
    For i = LBound(reservedWords) to UBound(reservedWords)
    strItem = ReplaceString(strItem,Cstr(reservedWords(i)),CStr(replacedWords(i)))
    Next i
    endif

    ReplaceReservedWords = strItem

    ‘ ReplaceReservedWords = ReplaceString(strItem,strPercent,strReplacedPercent)
    End Sub

    Function getFormattedCellString( strValue as String, oCell as Object, bEnable as Boolean) As String
    Dim strRet as String
    strRet = strValue

    if(bEnable = constON) then
    ‘oCell.CharPostureAsian
    if oCell.CharPosture = com.sun.star.awt.FontSlant.ITALIC then
    strRet = “\textit{” + strRet + “}”
    endif

    if oCell.CharWeight = com.sun.star.awt.FontWeight.BOLD then
    strRet = “\textbf{” + strRet + “}”
    endif
    endif

    getFormattedCellString = strRet
    End Function

    REM ShowResults
    Function ShowResults(strTextTable As String)
    Dim oDialog As Object
    Dim oTextField As Object

    DialogLibraries.LoadLibrary( “Calc2LaTeX” )
    oDialog = createUnoDialog( DialogLibraries.Calc2LaTeX.ShowResults)

    ‘MoDialog = oDialog.getModel()
    oDialog.setTitle( “Table” )

    oTextField = oDialog.getControl(“TextField1”)
    oTextField.setText( strTextTable )

    ‘ select all strings
    Dim oSelection
    oSelection = createUnoStruct( “com.sun.star.awt.Selection” )
    With oSelection
    .Min = 0
    .Max = len(strTextTable)
    End With
    oTextField.setSelection(oSelection)

    oDialog.execute()
    End Function

    public oDialog As Object
    REM SETTINGS UNDER DEVELOPMENT
    Function Settings2 () As ReturnValueOfSettings

    Dim oControl As Object

    Dim aRetvalue As ReturnValueOfSettings

    DialogLibraries.LoadLibrary( “Calc2LaTeX” )
    oDialog = createUnoDialog( DialogLibraries.Calc2LaTeX.DlgOption)
    oDialog.setTitle( “Settings” )
    oDialog.getControl(“lblVersion”).setText(strAppName +strEoL + strVersion + strEoL + strEoL + user_charset + strEoL + user_linefeed)

    ‘ load settings
    ‘On Error GoTo ERR1
    oDialog.getControl(“textLabelName”).setText(aGlobalSetting_labelString)
    oDialog.getControl(“textCaption”).setText(aGlobalSetting_captionString)

    Select Case aGlobalSetting_tabularEnable
    Case 0
    oDialog.getControl(“optTableAll”).State =True
    Case else
    oDialog.getControl(“optTableTabular”).State =True
    End Select
    changeTableStatus()

    Select Case aGlobalSetting_captionPosition
    Case 0
    oDialog.getControl(“optCaptionTop”).State =True
    Case else
    oDialog.getControl(“optCaptionBottom”).State =True
    End Select

    Select Case aGlobalSetting_horizontalPosition
    case 0
    oDialog.getControl(“optHpNone”).State=true
    Case 1
    oDialog.getControl(“optHpLeft”).State=true
    Case 2
    oDialog.getControl(“optHpCenter”).State = true
    Case else
    oDialog.getControl(“optHpRight”).State = true
    End Select

    Select Case aGlobalSetting_insertPosition
    Case 0
    oDialog.getControl(“optIpHere”).State=true
    Case 1
    oDialog.getControl(“optIpTop”).State=true
    Case 2
    oDialog.getControl(“optIpBottom”).State=true
    Case else
    oDialog.getControl(“optIpOnePage”).State=true
    End Select

    Select Case aGlobalSetting_ruledLine
    Case 0
    oDialog.getControl(“ruled line on”).State=true
    Case else
    oDialog.getControl(“ruled line off”).State=true
    End Select

    Select Case aGlobalSetting_formatCells
    Case 0
    oDialog.getControl(“FormatCellsOn”).State=true
    Case else
    oDialog.getControl(“FormatCellsOff”).State=true
    End Select

    Select Case aGlobalSetting_replaceReservedWord
    Case 0
    oDialog.getControl(“ReplaceReservedWordsOn”).State=true
    Case else
    oDialog.getControl(“ReplaceReservedWordsOff”).State=true
    End Select

    Select Case aGlobalSetting_outputResult
    Case 0
    oDialog.getControl(“OptResultWindow”).State=true
    Case Else
    oDialog.getControl(“optResultFile”).State=true
    End Select

    If oDialog.execute() 1 then
    oDocument.removeActionLock
    stop
    end if

    aGlobalSetting_labelString = oDialog.getControl(“textLabelName”).getText
    aGlobalSetting_captionString = oDialog.getControl(“textCaption”).getText

    ‘ \begin{center}…\end{center} and so on
    ‘ NOTICE oOption.State == 1 or True ? (See Developer’s Guide pp768)
    If oDialog.getControl(“optHpLeft”).State=true Then
    ‘ strTextTable = strBeginLeft + strEoL + strTextTable + strEndLeft + strEoL
    aRetValue.strTableHead = strBeginLeft + strEoL
    aRetValue.strTableTail = strEndLeft + strEoL
    aGlobalSetting_horizontalPosition = 1
    Elseif oDialog.getControl(“optHpCenter”).State = true Then
    ‘strTextTable = strBeginCenter + strEoL + strTextTable + strEndCenter + strEoL
    aRetValue.strTableHead = strBeginCenter + strEoL
    aRetValue.strTableTail = strEndCenter + strEoL
    aGlobalSetting_horizontalPosition = 2
    Elseif oDialog.getControl(“optHpRight”).State = true Then
    ‘ strTextTable = strBeginRight + strEoL + strTextTable + strEndRight + strEoL
    aRetValue.strTableHead = strBeginRight + strEoL
    aRetValue.strTableTail = strEndRight + strEoL
    aGlobalSetting_horizontalPosition = 3
    Else
    ‘ No Horizontal option
    aRetValue.strTableHead = “”
    aRetValue.strTableTail = “”
    aGlobalSetting_horizontalPosition = 0
    Endif

    ‘ \caption
    If oDialog.getControl(“optCaptionTop”).State =True Then
    ‘ strTextTable = strHeadCaption + oDialog.getControl(“textCaption”).getText + _
    ‘ strTailCaption + strEoL +strTextTable
    aRetValue.strTableHead = strHeadCaption + oDialog.getControl(“textCaption”).getText + strTailCaption + strEoL + aRetValue.strTableHead
    aGlobalSetting_captionPosition = 0
    Else
    ‘ strTextTable = strTextTable + strHeadCaption + oDialog.getControl(“textCaption”).getText + _
    ‘ strTailCaption+ strEoL
    aRetValue.strTableTail = aRetValue.strTableTail + strHeadCaption + oDialog.getControl(“textCaption”).getText + strTailCaption+ strEoL
    aGlobalSetting_captionPosition = 1
    Endif

    ‘ \label
    ‘ strTextTable = strTextTable + strHeadLabel + oDialog.getControl(“textLabelName”).getText + strTailLabel + strEoL
    aRetValue.strTableTail = aRetValue.strTableTail + strHeadLabel + oDialog.getControl(“textLabelName”).getText + strTailLabel + strEoL

    ‘ \begin{table}…\end{table}
    Dim strTableOption As String

    If oDialog.getControl(“optIpHere”).State=true Then
    aGlobalSetting_insertPosition = 0
    strTableOption = strTableHere
    Elseif oDialog.getControl(“optIpTop”).State=true Then
    aGlobalSetting_insertPosition = 1
    strTableOption = strTableTop
    Elseif oDialog.getControl(“optIpBottom”).State=true Then
    strTableOption = strTableBottom
    aGlobalSetting_insertPosition = 2
    Else
    strTableOption = strTableUseOnePage
    aGlobalSetting_insertPosition = 3
    Endif

    If oDialog.getControl(“ruled line on”).State=true Then
    aRetValue.bLineONOFF = constON
    aGlobalSetting_ruledLine = 0
    else
    aRetValue.bLineONOFF = constOFF
    aGlobalSetting_ruledLine = 1
    endif

    If oDialog.getControl(“ReplaceReservedWordsOn”).State=true Then
    aRetValue.bEnableReplaceWords = constON
    aGlobalSetting_replaceReservedWord = 0
    else
    aRetValue.bEnableReplaceWords = constOFF
    aGlobalSetting_replaceReservedWord = 1
    endif

    If oDialog.getControl(“FormatCellsOn”).State=true Then
    aRetValue.bEnableFormattingCells = constON
    aGlobalSetting_formatCells = 0
    else
    aRetValue.bEnableFormattingCells = constOFF
    aGlobalSetting_formatCells = 1
    endif

    ‘ strTextTable = strBeginTable + strTableOption + strEoL + strTextTable + strEndTable + strEoL

    aRetValue.strTableHead = strBeginTable + strTableOption + strEoL + aRetValue.strTableHead
    aRetValue.strTableTail = aRetValue.strTableTail +strEndTable + strEoL

    If oDialog.getControl(“optTableAll”).State=true Then
    aGlobalSetting_tabularEnable = 0
    else
    aRetValue.strTableHead = “”
    aRetValue.strTableTail = “”
    aGlobalSetting_tabularEnable = 1
    end if

    If oDialog.getControl(“OptResultWindow”).State=true then
    aRetValue.bResultWindow = constON
    aGlobalSetting_outputResult = 0
    Else
    aRetValue.bResultWindow = constOFF
    aGlobalSetting_outputResult = 1
    End If

    ‘return
    ‘ Settings = strTextTable
    Settings2 = aRetValue

    ‘ exit function
    ‘ERR1:
    ‘ aGlobalSetting.labelString = “”
    ‘ resume Next
    End Function

    REM SETTINGS
    Function Settings (strTextTable As String) As String
    Dim oDialog As Object
    Dim oControl As Object

    DialogLibraries.LoadLibrary( “Calc2LaTeX” )
    oDialog = createUnoDialog( DialogLibraries.Calc2LaTeX.DlgOption)
    oDialog.setTitle( “Settings” )
    oDialog.getControl(“lblVersion”).setText(strAppName +strEoL + strVersion)

    oDialog.execute()

    ‘ \begin{center}…\end{center} and so on
    ‘ NOTICE oOption.State == 1 or True ? (See Developer’s Guide pp768)
    If oDialog.getControl(“optHpLeft”).State=true Then
    strTextTable = strBeginLeft + strEoL + strTextTable + strEndLeft + strEoL
    Elseif oDialog.getControl(“optHpCenter”).State = true Then
    strTextTable = strBeginCenter + strEoL + strTextTable + strEndCenter + strEoL
    Elseif oDialog.getControl(“optHpRight”).State = true Then
    strTextTable = strBeginRight + strEoL + strTextTable + strEndRight + strEoL
    Endif

    ‘ \caption
    If oDialog.getControl(“optCaptionTop”).State =True Then
    strTextTable = strHeadCaption + oDialog.getControl(“textCaption”).getText + _
    strTailCaption + strEoL +strTextTable
    Else
    strTextTable = strTextTable + strHeadCaption + oDialog.getControl(“textCaption”).getText + _
    strTailCaption+ strEoL
    Endif

    ‘ \label
    strTextTable = strTextTable + strHeadLabel + oDialog.getControl(“textLabelName”).getText + strTailLabel + strEoL

    ‘ \begin{table}…\end{table}
    Dim strTableOption As String

    If oDialog.getControl(“optIpHere”).State=true Then
    strTableOption = strTableHere
    Elseif oDialog.getControl(“optIpTop”).State=true Then
    strTableOption = strTableTop
    Elseif oDialog.getControl(“optIpBottom”).State=true Then
    strTableOption = strTableBottom
    Else
    strTableOption = strTableUseOnePage
    Endif

    strTextTable = strBeginTable + strTableOption + strEoL + strTextTable + strEndTable + strEoL

    ‘return
    Settings = strTextTable
    End Function

    Sub Cancel
    oDocument.removeActionLock
    Stop
    End Sub

    Sub outputResultWithFileDialog(strMessage as string)
    Dim oFilePicker As Object

    oFilePicker = createUnoService( “com.sun.star.ui.dialogs.FilePicker” )

    ‘ Settings of the file dialog
    Dim nAny(0)
    nAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION
    ‘ nAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE
    oFilePicker.Initialize( nAny() )

    Dim nContentID, nAction As Integer
    nContentID = com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION
    oFilePicker.setValue( nContentID, nAction, True )

    oFilePicker.appendFilter( “TeX (*.tex)”, “*.tex” )
    oFilePicker.appendFilter( “All Files (*.*)”, “*.*” )
    oFilePicker.setCurrentFilter( “TeX (*.tex)” )

    ‘ oFilePicker.setDisplayDirectory(dialogDirectory)
    if dialogDirectory “” Then
    oFilePicker.setDisplayDirectory(dialogDirectory)
    oFilePicker.setDefaultName(dialogFilename)
    end if

    ‘ Show the dialog & output the result
    If oFilePicker.execute() = 1 Then
    dialogDirectory = oFilePicker.getDisplayDirectory()

    Dim sFiles()
    Dim sFileURL As String
    sFiles() = oFilePicker.getFiles()
    sFileURL = sFiles(0)

    ‘ save the filename for the next running
    GlobalScope.BasicLibraries.LoadLibrary(“Tools”)
    dialogFilename = FileNameOutOfPath(sFileURL)

    output2File(sFileURL, strMessage)
    End If
    End Sub

    Sub output2File(strFileURL as string, strMessage as string)
    Dim oFileAcc As Object
    Dim oOutputStream As Object
    Dim vData As Variant

    oFileAcc = CreateUnoService(“com.sun.star.ucb.SimpleFileAccess”)
    oOutputStream = CreateUnoService(“com.sun.star.io.TextOutputStream”)
    oOutputStream.setEncoding(user_charset)

    vData = oFileAcc.openFileWrite(strFileURL)
    vData.truncate()
    oOutputStream.setOutputStream(vData)

    oOutputStream.writeString( strMessage )
    oOutputStream.closeOutput()
    End Sub

    Sub changeTableStatus()
    Dim switchStatus as boolean

    If oDialog.getControl(“optTableAll”).State =True then
    switchStatus = true
    Else
    switchStatus = false
    end if

    Dim tableControls as Variant
    tableControls = Array(“Label1″,”textLabelName”,”optCaptionTop”,”optCaptionBottom”, _
    “FrameControl3″,”textCaption”, _
    “FrameControl1″,”optHpLeft”,”optHpCenter”,”optHpRight”,”optHpNone”, _
    “FrameControl2″,”optIpHere”,”optIpTop”,”optIpBottom”,”optIpOnePage”)

    Dim i as integer
    For i = LBound(tableControls) to UBound(tableControls)
    oDialog.getControl( CStr(tableControls(i)) ).Enable=switchStatus
    Next i
    End sub

    \end{document}

  3. Trương Quốc Đại

    Dạ em cảm ơn Thầy đã tận tình giúp đỡ! em sẽ chờ bài hướng dẫn LaTable của Thầy! Nhân dịp năm mới em xin chúc Thầy và gia đình thật nhiều sức khỏe, niềm vui và hạnh phúc!

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s