File: array_formula.py

package info (click to toggle)
xlsxwriter 3.1.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 56,308 kB
  • sloc: python: 51,511; javascript: 7,768; sh: 284; makefile: 195; perl: 75
file content (37 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#######################################################################
#
# Example of how to use Python and the XlsxWriter module to write
# simple array formulas.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

# Create a new workbook and add a worksheet
workbook = xlsxwriter.Workbook("array_formula.xlsx")
worksheet = workbook.add_worksheet()

# Write some test data.
worksheet.write("B1", 500)
worksheet.write("B2", 10)
worksheet.write("B5", 1)
worksheet.write("B6", 2)
worksheet.write("B7", 3)
worksheet.write("C1", 300)
worksheet.write("C2", 15)
worksheet.write("C5", 20234)
worksheet.write("C6", 21003)
worksheet.write("C7", 10000)


# Write an array formula that returns a single value
worksheet.write_formula("A1", "{=SUM(B1:C1*B2:C2)}")

# Same as above but more verbose.
worksheet.write_array_formula("A2:A2", "{=SUM(B1:C1*B2:C2)}")

# Write an array formula that returns a range of values
worksheet.write_array_formula("A5:A7", "{=TREND(C5:C7,B5:B7)}")

workbook.close()