- The added line is THIS COLOR.
- The deleted line is THIS COLOR.
- Go to OOobbs2/57.
** [[OOobbs2/57]] [#xb728d3f]
-''サマリ'': ひとつのシートのページ数の取得
-''環境'': Calc
-''状態'': 未解決
-''投稿者'': [[はにゃ?]]
-''投稿日'': 2007-08-01 (水) 03:13:17
*** 質問 [#nbdd9cfb]
シートごとに印刷もしくはエクスポートされるページ数を API を使って取得できないのかどうなのか分からなかったので。
PDF ファイルをマクロなどからエクスポートするとき、次のようにするとページ指定でエクスポートできる。PageRange と SelectionOnly を設定する。
aArg1(0).Name = "InitialView"
aArg1(0).Value = 0
aArg1(1).Name = "PageLayout"
aArg1(1).Value = 0
aArg1(2).Name = "PageRange"
aArg1(2).Value = "1-2"
aArgs(0).Name = "FilterName"
aArgs(0).Value = "calc_pdf_Export"
aArgs(1).Name = "Overwrite"
aArgs(1).Value = true
aArgs(2).Name = "FilterData"
aArgs(2).Value = aArg1()
aArgs(3).Name = "SelectionOnly"
aArgs(3).Value = true
シートごとのページ数が分かれば、シートごとに PDF ファイルをエクスポートできる。
シートごとのページ数を計算して、アクティブシートのページを "最初のページ番号 - 最後のページ番号" として表示する。
Sub Main
Dim oDoc As Object, oSheets As Object
Dim oSheet As Object
Dim nPages As Integer
oDoc = ThisComponent
oSheets = oDoc.getSheets()
oActiveSheet = oDoc.CurrentController.ActiveSheet
nSheetIndex = oActiveSheet.RangeAddress.Sheet
For i = 0 To nSheetIndex -1
oSheet = oSheets.getByIndex(i)
nPages = nPages + GetNumberOfPages( oSheet )
Next i
nSelected = GetNumberOfPages( oActiveSheet )
Select Case nSelected
Case 0
msgbox "no page"
Case 1
msgbox nPages +1
Case >1
msgbox nPages +1 & "-" & nPages + nSelected
End Select
End Sub
Function GetNumberOfPages( oLocSheet As Object ) As Integer
Dim oCursor As Object
Dim aUsedRange As New com.sun.star.table.CellRangeAddress
Dim aColumnBreaks()' As New com.sun.star.sheet.TablePageBreakData
Dim aRowBreaks()' As New com.sun.star.sheet.TablePageBreakData
Dim oSearchDesc As Object
Dim oRange As Object
Dim nCount As Long
Dim nWidth As Long, nHeight As Long
Dim nSC As Long, nEC As Long, nSR As Long, nER As Long
oCursor = oLocSheet.createCursor()
oCursor.gotoStartOfUsedArea( False )
oCursor.gotoEndOfUsedArea( True )
aUsedRange = oCursor.RangeAddress
aColumnBreaks = oLocSheet.ColumnPageBreaks
aRowBreaks = oLocSheet.RowPageBreaks
nWidth = 0
nHeight = 0
For i = 0 To UBound(aColumnBreaks)
If aColumnBreaks(i).Position >= aUsedRange.EndColumn Then
nWidth = i +1
Exit For
End If
Next i
For i = 0 To UBound(aRowBreaks)
If aRowBreaks(i).Position >= aUsedRange.EndRow Then
nHeight = i +1
Exit For
End If
Next i
oSearchDesc = oLocSheet.createSearchDescriptor()
oSearchDesc.SearchString = "."
oSearchDesc.SearchRegularExpression = true
nCount = 0
For i = 0 To nWidth -1
For j = 0 To nHeight -1
If i = 0 Then
nSC = 0
nEC = aColumnBreaks(i).Position
Else
nSC = aColumnBreaks(i-1).Position
nEC = aColumnBreaks(i).Position
End If
If j = 0 Then
nSR = 0
nER = aRowBreaks(j).Position
Else
nSR = aRowBreaks(j-1).Position
nER = aRowBreaks(j).Position
End If
oRange = oLocSheet.getCellRangeByPosition( _
nSC, nSR, nEC, nER )
oRanges = oRange.findAll(oSearchDesc)
If NOT IsNull(oRanges) Then
nCount = nCount +1
End If
Next j
Next i
If nCount > 0 Then
GetNumberOfPages = nWidth * nHeight
Else
GetNumberOfPages = 0
End If
End Function
PDF にエクスポートするときには、「空白ページを印刷しない」にチェックが入っていてもエクスポートされるのでそれに合うようにしてある。最初は空白ページがエクスポートされないと思ったが、そうではなかったので修正。使われていないシートが間にあるときにも対応。
次の場合の動作が不明。思いつけば改変。
-印刷しない図などがあるとき
-表示されていない行、列があるとき
-印刷不可能文字が入力されているとき
*** 回答 [#t51f5800]
#comment
*** 感想,コメント,メモ [#db3ef03e]
#comment