xmlWorkbook.go 19.0 KB
Newer Older
1
// Copyright 2016 - 2024 The excelize Authors. All rights reserved. Use of
xurime's avatar
xurime 已提交
2 3 4
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
5 6 7 8 9
// Package excelize providing a set of functions that allow you to write to and
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
xurime's avatar
xurime 已提交
10
// data. This library needs Go version 1.16 or later.
xurime's avatar
xurime 已提交
11

xurime's avatar
xurime 已提交
12 13
package excelize

xurime's avatar
xurime 已提交
14 15 16 17
import (
	"encoding/xml"
	"sync"
)
xurime's avatar
xurime 已提交
18

19 20
// xlsxRelationships describe references from parts to other internal resources
// in the package or to external resources.
xurime's avatar
xurime 已提交
21
type xlsxRelationships struct {
22
	mu            sync.Mutex
xurime's avatar
xurime 已提交
23 24
	XMLName       xml.Name           `xml:"http://schemas.openxmlformats.org/package/2006/relationships Relationships"`
	Relationships []xlsxRelationship `xml:"Relationship"`
xurime's avatar
xurime 已提交
25 26
}

xurime's avatar
xurime 已提交
27 28
// xlsxRelationship contains relations which maps id and XML.
type xlsxRelationship struct {
29 30 31 32
	ID         string `xml:"Id,attr"`
	Target     string `xml:",attr"`
	Type       string `xml:",attr"`
	TargetMode string `xml:",attr,omitempty"`
xurime's avatar
xurime 已提交
33 34
}

xurime's avatar
xurime 已提交
35 36 37
// xlsxWorkbook contains elements and attributes that encompass the data
// content of the workbook. The workbook's child elements each have their own
// subclause references.
xurime's avatar
xurime 已提交
38
type xlsxWorkbook struct {
39 40 41 42 43
	XMLName                xml.Name                 `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main workbook"`
	Conformance            string                   `xml:"conformance,attr,omitempty"`
	FileVersion            *xlsxFileVersion         `xml:"fileVersion"`
	FileSharing            *xlsxExtLst              `xml:"fileSharing"`
	WorkbookPr             *xlsxWorkbookPr          `xml:"workbookPr"`
44 45
	AlternateContent       *xlsxAlternateContent    `xml:"mc:AlternateContent"`
	DecodeAlternateContent *xlsxInnerXML            `xml:"http://schemas.openxmlformats.org/markup-compatibility/2006 AlternateContent"`
46
	WorkbookProtection     *xlsxWorkbookProtection  `xml:"workbookProtection"`
47 48
	BookViews              *xlsxBookViews           `xml:"bookViews"`
	Sheets                 xlsxSheets               `xml:"sheets"`
49
	FunctionGroups         *xlsxFunctionGroups      `xml:"functionGroups"`
50 51 52 53 54 55 56 57 58 59 60 61
	ExternalReferences     *xlsxExternalReferences  `xml:"externalReferences"`
	DefinedNames           *xlsxDefinedNames        `xml:"definedNames"`
	CalcPr                 *xlsxCalcPr              `xml:"calcPr"`
	OleSize                *xlsxExtLst              `xml:"oleSize"`
	CustomWorkbookViews    *xlsxCustomWorkbookViews `xml:"customWorkbookViews"`
	PivotCaches            *xlsxPivotCaches         `xml:"pivotCaches"`
	SmartTagPr             *xlsxExtLst              `xml:"smartTagPr"`
	SmartTagTypes          *xlsxExtLst              `xml:"smartTagTypes"`
	WebPublishing          *xlsxExtLst              `xml:"webPublishing"`
	FileRecoveryPr         *xlsxFileRecoveryPr      `xml:"fileRecoveryPr"`
	WebPublishObjects      *xlsxExtLst              `xml:"webPublishObjects"`
	ExtLst                 *xlsxExtLst              `xml:"extLst"`
62 63
}

64 65 66 67
// xlsxFileRecoveryPr maps sheet recovery information. This element defines
// properties that track the state of the workbook file, such as whether the
// file was saved during a crash, or whether it should be opened in auto-recover
// mode.
xurime's avatar
xurime 已提交
68
type xlsxFileRecoveryPr struct {
69 70 71 72 73 74
	AutoRecover     bool `xml:"autoRecover,attr,omitempty"`
	CrashSave       bool `xml:"crashSave,attr,omitempty"`
	DataExtractLoad bool `xml:"dataExtractLoad,attr,omitempty"`
	RepairLoad      bool `xml:"repairLoad,attr,omitempty"`
}

75 76 77 78 79 80 81 82 83
// xlsxWorkbookProtection directly maps the workbookProtection element. This
// element specifies options for protecting data in the workbook. Applications
// might use workbook protection to prevent anyone from accidentally changing,
// moving, or deleting important data. This protection can be ignored by
// applications which choose not to support this optional protection mechanism.
// When a password is to be hashed and stored in this element, it shall be
// hashed as defined below, starting from a UTF-16LE encoded string value. If
// there is a leading BOM character (U+FEFF) in the encoded password it is
// removed before hash calculation.
xurime's avatar
xurime 已提交
84
type xlsxWorkbookProtection struct {
85 86 87 88 89 90 91 92 93 94 95 96 97
	LockRevision           bool   `xml:"lockRevision,attr,omitempty"`
	LockStructure          bool   `xml:"lockStructure,attr,omitempty"`
	LockWindows            bool   `xml:"lockWindows,attr,omitempty"`
	RevisionsAlgorithmName string `xml:"revisionsAlgorithmName,attr,omitempty"`
	RevisionsHashValue     string `xml:"revisionsHashValue,attr,omitempty"`
	RevisionsSaltValue     string `xml:"revisionsSaltValue,attr,omitempty"`
	RevisionsSpinCount     int    `xml:"revisionsSpinCount,attr,omitempty"`
	WorkbookAlgorithmName  string `xml:"workbookAlgorithmName,attr,omitempty"`
	WorkbookHashValue      string `xml:"workbookHashValue,attr,omitempty"`
	WorkbookSaltValue      string `xml:"workbookSaltValue,attr,omitempty"`
	WorkbookSpinCount      int    `xml:"workbookSpinCount,attr,omitempty"`
}

98 99 100
// xlsxFileVersion directly maps the fileVersion element. This element defines
// properties that track which version of the application accessed the data and
// source code contained in the file.
xurime's avatar
xurime 已提交
101 102
type xlsxFileVersion struct {
	AppName      string `xml:"appName,attr,omitempty"`
103
	CodeName     string `xml:"codeName,attr,omitempty"`
xurime's avatar
xurime 已提交
104 105 106 107 108
	LastEdited   string `xml:"lastEdited,attr,omitempty"`
	LowestEdited string `xml:"lowestEdited,attr,omitempty"`
	RupBuild     string `xml:"rupBuild,attr,omitempty"`
}

109 110 111
// xlsxWorkbookPr directly maps the workbookPr element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main This element
// defines a collection of workbook properties.
xurime's avatar
xurime 已提交
112
type xlsxWorkbookPr struct {
113
	Date1904                   bool   `xml:"date1904,attr,omitempty"`
114 115
	ShowObjects                string `xml:"showObjects,attr,omitempty"`
	ShowBorderUnselectedTables *bool  `xml:"showBorderUnselectedTables,attr"`
116 117
	FilterPrivacy              bool   `xml:"filterPrivacy,attr,omitempty"`
	PromptedSolutions          bool   `xml:"promptedSolutions,attr,omitempty"`
118 119 120 121 122 123 124 125
	ShowInkAnnotation          *bool  `xml:"showInkAnnotation,attr"`
	BackupFile                 bool   `xml:"backupFile,attr,omitempty"`
	SaveExternalLinkValues     *bool  `xml:"saveExternalLinkValues,attr"`
	UpdateLinks                string `xml:"updateLinks,attr,omitempty"`
	CodeName                   string `xml:"codeName,attr,omitempty"`
	HidePivotFieldList         bool   `xml:"hidePivotFieldList,attr,omitempty"`
	ShowPivotChartFilter       bool   `xml:"showPivotChartFilter,attr,omitempty"`
	AllowRefreshQuery          bool   `xml:"allowRefreshQuery,attr,omitempty"`
126
	PublishItems               bool   `xml:"publishItems,attr,omitempty"`
127 128
	CheckCompatibility         bool   `xml:"checkCompatibility,attr,omitempty"`
	AutoCompressPictures       *bool  `xml:"autoCompressPictures,attr"`
129
	RefreshAllConnections      bool   `xml:"refreshAllConnections,attr,omitempty"`
130
	DefaultThemeVersion        string `xml:"defaultThemeVersion,attr,omitempty"`
131 132
}

133 134 135 136
// xlsxBookViews directly maps the bookViews element. This element specifies the
// collection of workbook views of the enclosing workbook. Each view can specify
// a window position, filter options, and other configurations. There is no
// limit on the number of workbook views that can be defined for a workbook.
xurime's avatar
xurime 已提交
137 138 139 140
type xlsxBookViews struct {
	WorkBookView []xlsxWorkBookView `xml:"workbookView"`
}

141 142 143
// xlsxWorkBookView directly maps the workbookView element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main This element
// specifies a single Workbook view.
xurime's avatar
xurime 已提交
144
type xlsxWorkBookView struct {
145 146 147 148 149 150 151 152 153 154 155 156 157
	Visibility             string  `xml:"visibility,attr,omitempty"`
	Minimized              bool    `xml:"minimized,attr,omitempty"`
	ShowHorizontalScroll   *bool   `xml:"showHorizontalScroll,attr"`
	ShowVerticalScroll     *bool   `xml:"showVerticalScroll,attr"`
	ShowSheetTabs          *bool   `xml:"showSheetTabs,attr"`
	XWindow                string  `xml:"xWindow,attr,omitempty"`
	YWindow                string  `xml:"yWindow,attr,omitempty"`
	WindowWidth            int     `xml:"windowWidth,attr,omitempty"`
	WindowHeight           int     `xml:"windowHeight,attr,omitempty"`
	TabRatio               float64 `xml:"tabRatio,attr,omitempty"`
	FirstSheet             int     `xml:"firstSheet,attr,omitempty"`
	ActiveTab              int     `xml:"activeTab,attr,omitempty"`
	AutoFilterDateGrouping *bool   `xml:"autoFilterDateGrouping,attr"`
xurime's avatar
xurime 已提交
158 159 160
}

// xlsxSheets directly maps the sheets element from the namespace
161
// http://schemas.openxmlformats.org/spreadsheetml/2006/main.
xurime's avatar
xurime 已提交
162 163 164 165
type xlsxSheets struct {
	Sheet []xlsxSheet `xml:"sheet"`
}

166 167
// xlsxSheet defines a sheet in this workbook. Sheet data is stored in a
// separate part.
xurime's avatar
xurime 已提交
168 169
type xlsxSheet struct {
	Name    string `xml:"name,attr,omitempty"`
170
	SheetID int    `xml:"sheetId,attr,omitempty"`
xurime's avatar
xurime 已提交
171
	ID      string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr"`
xurime's avatar
xurime 已提交
172 173 174
	State   string `xml:"state,attr,omitempty"`
}

175 176 177 178 179 180 181 182 183 184 185
// xlsxFunctionGroup represents a single function group.
type xlsxFunctionGroup struct {
	Name string `xml:"name,attr"`
}

// xlsxFunctionGroups defines the collection of function groups for the workbook.
type xlsxFunctionGroups struct {
	BuiltInGroupCount *int                `xml:"builtInGroupCount,attr"`
	FunctionGroup     []xlsxFunctionGroup `xml:"functionGroup"`
}

186 187
// xlsxExternalReferences directly maps the externalReferences element of the
// external workbook references part.
188 189 190 191
type xlsxExternalReferences struct {
	ExternalReference []xlsxExternalReference `xml:"externalReference"`
}

192 193
// xlsxExternalReference directly maps the externalReference element of the
// external workbook references part.
194 195 196 197
type xlsxExternalReference struct {
	RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
}

198 199
// xlsxPivotCaches element enumerates pivot cache definition parts used by pivot
// tables and formulas in this workbook.
200 201 202 203 204 205
type xlsxPivotCaches struct {
	PivotCache []xlsxPivotCache `xml:"pivotCache"`
}

// xlsxPivotCache directly maps the pivotCache element.
type xlsxPivotCache struct {
206
	CacheID int    `xml:"cacheId,attr"`
207 208 209 210
	RID     string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
}

// extLst element provides a convention for extending spreadsheetML in
211 212 213 214 215
// predefined locations. The locations shall be denoted with the extLst element,
// and are called extension lists. Extension list locations within the markup
// document are specified in the markup specification and can be used to store
// extensions to the markup specification, whether those are future version
// extensions of the markup specification or are private extensions implemented
216
// independently of the markup specification. Markup within an extension might
217
// not be understood by a consumer.
218 219
type xlsxExtLst struct {
	Ext string `xml:",innerxml"`
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

// xlsxExt represents a the future feature data storage area. Each extension
// within an extension list shall be contained within an ext element.
// Extensions shall be versioned by namespace, using the uri attribute, and
// shall be allowed to appear in any order within the extension list. Any
// number of extensions shall be allowed within an extension list.
type xlsxExt struct {
	XMLName xml.Name `xml:"ext"`
	URI     string   `xml:"uri,attr"`
	Content string   `xml:",innerxml"`
	xmlns   []xml.Attr
}

// xlsxAlternateContent is a container for a sequence of multiple
// representations of a given piece of content. The program reading the file
// should only process one of these, and the one chosen should be based on
// which conditions match.
type xlsxAlternateContent struct {
	XMLNSMC string `xml:"xmlns:mc,attr,omitempty"`
	Content string `xml:",innerxml"`
}

// xlsxChoice element shall be an element in the Markup Compatibility namespace
// with local name "Choice". Parent elements of Choice elements shall be
// AlternateContent elements.
type xlsxChoice struct {
	XMLName    xml.Name `xml:"mc:Choice"`
248
	XMLNSA14   string   `xml:"xmlns:a14,attr,omitempty"`
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	XMLNSSle15 string   `xml:"xmlns:sle15,attr,omitempty"`
	Requires   string   `xml:"Requires,attr,omitempty"`
	Content    string   `xml:",innerxml"`
}

// xlsxFallback element shall be an element in the Markup Compatibility
// namespace with local name "Fallback". Parent elements of Fallback elements
// shall be AlternateContent elements.
type xlsxFallback struct {
	XMLName xml.Name `xml:"mc:Fallback"`
	Content string   `xml:",innerxml"`
}

// xlsxInnerXML holds parts of XML content currently not unmarshal.
type xlsxInnerXML struct {
	Content string `xml:",innerxml"`
}

// decodeExtLst defines the structure used to parse the extLst element
// of the future feature data storage area.
type decodeExtLst struct {
	XMLName xml.Name   `xml:"extLst"`
	Ext     []*xlsxExt `xml:"ext"`
}

// decodeExt defines the structure used to parse the ext element.
type decodeExt struct {
	URI     string `xml:"uri,attr,omitempty"`
	Content string `xml:",innerxml"`
278 279
}

280 281 282 283
// xlsxDefinedNames directly maps the definedNames element. This element defines
// the collection of defined names for this workbook. Defined names are
// descriptive names to represent cells, ranges of cells, formulas, or constant
// values. Defined names can be used to represent a range on any worksheet.
xurime's avatar
xurime 已提交
284 285 286 287
type xlsxDefinedNames struct {
	DefinedName []xlsxDefinedName `xml:"definedName"`
}

288 289 290
// xlsxDefinedName directly maps the definedName element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main This element
// defines a defined name within this workbook. A defined name is descriptive
291
// text that is used to represent a cell, range of cells, formula, or constant
292
// value. For a descriptions of the attributes see https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.definedname
xurime's avatar
xurime 已提交
293 294 295 296
type xlsxDefinedName struct {
	Comment           string `xml:"comment,attr,omitempty"`
	CustomMenu        string `xml:"customMenu,attr,omitempty"`
	Description       string `xml:"description,attr,omitempty"`
297 298
	Function          bool   `xml:"function,attr,omitempty"`
	FunctionGroupID   int    `xml:"functionGroupId,attr,omitempty"`
xurime's avatar
xurime 已提交
299
	Help              string `xml:"help,attr,omitempty"`
300
	Hidden            bool   `xml:"hidden,attr,omitempty"`
301
	LocalSheetID      *int   `xml:"localSheetId,attr"`
302 303
	Name              string `xml:"name,attr,omitempty"`
	PublishToServer   bool   `xml:"publishToServer,attr,omitempty"`
xurime's avatar
xurime 已提交
304 305 306 307 308
	ShortcutKey       string `xml:"shortcutKey,attr,omitempty"`
	StatusBar         string `xml:"statusBar,attr,omitempty"`
	VbProcedure       bool   `xml:"vbProcedure,attr,omitempty"`
	WorkbookParameter bool   `xml:"workbookParameter,attr,omitempty"`
	Xlm               bool   `xml:"xml,attr,omitempty"`
309
	Data              string `xml:",chardata"`
xurime's avatar
xurime 已提交
310 311
}

312
// xlsxCalcPr directly maps the calcPr element. This element defines the
313 314 315
// collection of properties the application uses to record calculation status
// and details. Calculation is the process of computing formulas and then
// displaying the results as values in the cells that contain the formulas.
xurime's avatar
xurime 已提交
316
type xlsxCalcPr struct {
317 318 319 320
	CalcCompleted         bool    `xml:"calcCompleted,attr,omitempty"`
	CalcID                string  `xml:"calcId,attr,omitempty"`
	CalcMode              string  `xml:"calcMode,attr,omitempty"`
	CalcOnSave            bool    `xml:"calcOnSave,attr,omitempty"`
321
	ConcurrentCalc        *bool   `xml:"concurrentCalc,attr"`
322 323 324 325 326 327 328 329
	ConcurrentManualCount int     `xml:"concurrentManualCount,attr,omitempty"`
	ForceFullCalc         bool    `xml:"forceFullCalc,attr,omitempty"`
	FullCalcOnLoad        bool    `xml:"fullCalcOnLoad,attr,omitempty"`
	FullPrecision         bool    `xml:"fullPrecision,attr,omitempty"`
	Iterate               bool    `xml:"iterate,attr,omitempty"`
	IterateCount          int     `xml:"iterateCount,attr,omitempty"`
	IterateDelta          float64 `xml:"iterateDelta,attr,omitempty"`
	RefMode               string  `xml:"refMode,attr,omitempty"`
xurime's avatar
xurime 已提交
330
}
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

// xlsxCustomWorkbookViews defines the collection of custom workbook views that
// are defined for this workbook. A customWorkbookView is similar in concept to
// a workbookView in that its attributes contain settings related to the way
// that the workbook should be displayed on a screen by a spreadsheet
// application.
type xlsxCustomWorkbookViews struct {
	CustomWorkbookView []xlsxCustomWorkbookView `xml:"customWorkbookView"`
}

// xlsxCustomWorkbookView directly maps the customWorkbookView element. This
// element specifies a single custom workbook view. A custom workbook view
// consists of a set of display and print settings that you can name and apply
// to a workbook. You can create more than one custom workbook view of the same
// workbook. Custom Workbook Views are not required in order to construct a
// valid SpreadsheetML document, and are not necessary if the document is never
// displayed by a spreadsheet application, or if the spreadsheet application has
// a fixed display for workbooks. However, if a spreadsheet application chooses
// to implement configurable display modes, the customWorkbookView element
// should be used to persist the settings for those display modes.
type xlsxCustomWorkbookView struct {
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	ActiveSheetID        *int     `xml:"activeSheetId,attr"`
	AutoUpdate           *bool    `xml:"autoUpdate,attr"`
	ChangesSavedWin      *bool    `xml:"changesSavedWin,attr"`
	GUID                 *string  `xml:"guid,attr"`
	IncludeHiddenRowCol  *bool    `xml:"includeHiddenRowCol,attr"`
	IncludePrintSettings *bool    `xml:"includePrintSettings,attr"`
	Maximized            *bool    `xml:"maximized,attr"`
	MergeInterval        int      `xml:"mergeInterval,attr"`
	Minimized            *bool    `xml:"minimized,attr"`
	Name                 *string  `xml:"name,attr"`
	OnlySync             *bool    `xml:"onlySync,attr"`
	PersonalView         *bool    `xml:"personalView,attr"`
	ShowComments         *string  `xml:"showComments,attr"`
	ShowFormulaBar       *bool    `xml:"showFormulaBar,attr"`
	ShowHorizontalScroll *bool    `xml:"showHorizontalScroll,attr"`
	ShowObjects          *string  `xml:"showObjects,attr"`
	ShowSheetTabs        *bool    `xml:"showSheetTabs,attr"`
	ShowStatusbar        *bool    `xml:"showStatusbar,attr"`
	ShowVerticalScroll   *bool    `xml:"showVerticalScroll,attr"`
	TabRatio             *float64 `xml:"tabRatio,attr"`
	WindowHeight         *int     `xml:"windowHeight,attr"`
	WindowWidth          *int     `xml:"windowWidth,attr"`
	XWindow              *int     `xml:"xWindow,attr"`
	YWindow              *int     `xml:"yWindow,attr"`
376
}
377 378 379 380

// DefinedName directly maps the name for a cell or cell range on a
// worksheet.
type DefinedName struct {
381 382 383 384
	Name     string
	Comment  string
	RefersTo string
	Scope    string
385 386 387 388
}

// WorkbookPropsOptions directly maps the settings of workbook proprieties.
type WorkbookPropsOptions struct {
389 390 391
	Date1904      *bool
	FilterPrivacy *bool
	CodeName      *string
392
}
G
Gin 已提交
393 394 395

// WorkbookProtectionOptions directly maps the settings of workbook protection.
type WorkbookProtectionOptions struct {
396 397 398 399
	AlgorithmName string
	Password      string
	LockStructure bool
	LockWindows   bool
G
Gin 已提交
400
}