2017-08-24 522 views
0

セルA1に式"=C1+$D$1"があります。私はxlwingsを使用してこの式をA3にコピーしてコピーします(相対的なセル参照を保持します)。私はセルA3の貼り付けられた数式が"=C1+$D$1"の代わりに"=C3+$D$1"であると予想します。Python xlwings相対セル参照を使用したコピー貼り付け式

貼り付け範囲に基づいて数式を調整するフラグや関数がありますか?そうでない場合は、貼り付け前に式自体を処理するのが最善の解決策だろうと思いますか?

rng_to_paste = ws.range('A1').options(ndim=1).formula 
ws.range('A3').options(ndim=1).formula = rng_to_paste 
+0

そのまま式の値をコピーします。実際には、数式自体を処理するか、より簡単に**新しい**式をあなたの例の現在の行に反映させる必要があります。 – stovfl

答えて

0

私の回避策は、私のためにコピー貼り付けを行うようにExcelマクロを呼び出すことでした。ワークシートとRangeオブジェクトをマクロに渡すときにエラーが発生したので、以下のコードでは文字列識別子のみを使用します。

パイソン

def copypaste_range(wb_string, 
       ws_source_string, 
       rng_to_copy_string, 
       ws_destination_string, 
       rng_to_paste_string): 
import xlwings as xw 
xw.App.display_alerts = False 
folder = r'D:\My Documents' 
xls_path = folder + r'\xlwings_macros.xlsb' 
wb_macros = xw.Book(xls_path) 
wb_macro = wb_macros.macro('copypaste_range') 
wb_macro(wb_string, 
     ws_source_string, 
     rng_to_copy_string, 
     ws_destination_string, 
     rng_to_paste_string) 
xw.App.display_alerts = True 
wb_macros.close() 

VBAは

Public Sub copypaste_range(wb_string, _ 
         ws_source_string, rng_to_copy_string, _ 
         ws_destination_string, rng_to_paste_string) 

Dim wb As Workbook 
Dim fso As New FileSystemObject 
If Not IsWorkBookOpen(wb_string) Then 
    Set wb = Workbooks.Open(fileName:=wb_string) 
Else 
    wb_string = fso.GetFileName(wb_string) 
    Set wb = Workbooks(wb_string) 
End If 

Dim rng_to_copy As Range 
Dim rng_to_paste As Range 
Set rng_to_copy = wb.Sheets(ws_source_string).Range(rng_to_copy_string) 
Set rng_to_paste = wb.Sheets(ws_destination_string).Range(rng_to_paste_string) 

rng_to_copy.Copy _ 
    Destination:=rng_to_paste 

End Sub 
0

あなたが調整され、式(S)との範囲内で暗黙的に割り当てられているセルを更新する範囲の式のプロパティを割り当てることができます。しかし、この方法では、xlwingsはどこから式をコピーしたか分からないため、割り当てられた数式に対して行/列を増やすことしかできません。

xw.sheets[0].range('A1:A5').value = [[i] for i in range(5)] 
xw.sheets[0].range('B1:C5').formula = [['=A1+1','=B1*10']] 

xw.sheets[0].range('A1:C5').value 
Out[3]: 
[[0.0, 1.0, 10.0], 
[1.0, 2.0, 20.0], 
[2.0, 3.0, 30.0], 
[3.0, 4.0, 40.0], 
[4.0, 5.0, 50.0]] 
関連する問題