addPivotCache provides a function to create a pivot cache by given properties.
(opts *PivotTableOptions)
| 284 | |
| 285 | // addPivotCache provides a function to create a pivot cache by given properties. |
| 286 | func (f *File) addPivotCache(opts *PivotTableOptions) error { |
| 287 | // validate data range |
| 288 | dataSheet, coordinates, err := f.adjustRange(opts.pivotDataRange) |
| 289 | if err != nil { |
| 290 | return newPivotTableDataRangeError(err.Error()) |
| 291 | } |
| 292 | order, err := f.getTableFieldsOrder(opts) |
| 293 | if err != nil { |
| 294 | return newPivotTableDataRangeError(err.Error()) |
| 295 | } |
| 296 | topLeftCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1]) |
| 297 | bottomRightCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3]) |
| 298 | pc := xlsxPivotCacheDefinition{ |
| 299 | SaveData: false, |
| 300 | RefreshOnLoad: true, |
| 301 | CreatedVersion: pivotTableVersion, |
| 302 | RefreshedVersion: pivotTableRefreshedVersion, |
| 303 | MinRefreshableVersion: pivotTableVersion, |
| 304 | CacheSource: &xlsxCacheSource{ |
| 305 | Type: "worksheet", |
| 306 | WorksheetSource: &xlsxWorksheetSource{ |
| 307 | Ref: topLeftCell + ":" + bottomRightCell, |
| 308 | Sheet: dataSheet, |
| 309 | }, |
| 310 | }, |
| 311 | CacheFields: &xlsxCacheFields{}, |
| 312 | } |
| 313 | if opts.namedDataRange { |
| 314 | pc.CacheSource.WorksheetSource = &xlsxWorksheetSource{Name: opts.DataRange} |
| 315 | } |
| 316 | for _, name := range order { |
| 317 | pc.CacheFields.CacheField = append(pc.CacheFields.CacheField, &xlsxCacheField{ |
| 318 | Name: name, |
| 319 | SharedItems: &xlsxSharedItems{ContainsBlank: true, M: []xlsxMissing{{}}}, |
| 320 | }) |
| 321 | } |
| 322 | pc.CacheFields.Count = len(pc.CacheFields.CacheField) |
| 323 | pivotCache, err := xml.Marshal(pc) |
| 324 | f.saveFileList(opts.pivotCacheXML, pivotCache) |
| 325 | return err |
| 326 | } |
| 327 | |
| 328 | // addPivotTable provides a function to create a pivot table by given pivot |
| 329 | // table ID and properties. |