2024-01-09 20:56:20 +08:00
// Copyright 2016 - 2024 The excelize Authors. All rights reserved. Use of
2019-12-22 00:02:09 +08:00
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
2022-02-17 00:09:11 +08:00
// 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
2024-01-18 15:31:43 +08:00
// data. This library needs Go version 1.18 or later.
2019-12-22 00:02:09 +08:00
package excelize
import (
2022-03-05 14:48:34 +08:00
"encoding/xml"
2023-07-28 00:24:08 +08:00
"fmt"
2023-07-11 23:43:45 +08:00
"os"
2019-12-22 00:02:09 +08:00
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
func TestAddComment ( t * testing . T ) {
2019-12-22 00:02:09 +08:00
f , err := prepareTestBook1 ( )
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
2022-11-02 08:42:00 +08:00
s := strings . Repeat ( "c" , TotalCellChars + 1 )
2023-07-11 23:43:45 +08:00
assert . NoError ( t , f . AddComment ( "Sheet1" , Comment { Cell : "A30" , Author : s , Text : s , Paragraph : [ ] RichTextRun { { Text : s } , { Text : s } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "B7" , Author : "Excelize" , Text : s [ : TotalCellChars - 1 ] , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment." } } } ) )
2019-12-22 00:02:09 +08:00
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test add comment on not exists worksheet
2023-07-11 23:43:45 +08:00
assert . EqualError ( t , f . AddComment ( "SheetN" , Comment { Cell : "B7" , Author : "Excelize" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment." } } } ) , "sheet SheetN does not exist" )
2022-09-18 00:07:15 +08:00
// Test add comment on with illegal cell reference
2023-11-11 00:04:05 +08:00
assert . Equal ( t , newCellNameToCoordinatesError ( "A" , newInvalidCellNameError ( "A" ) ) , f . AddComment ( "Sheet1" , Comment { Cell : "A" , Author : "Excelize" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment." } } } ) )
2023-03-25 13:30:13 +08:00
comments , err := f . GetComments ( "Sheet1" )
2022-11-12 00:02:11 +08:00
assert . NoError ( t , err )
2023-03-25 13:30:13 +08:00
assert . Len ( t , comments , 2 )
comments , err = f . GetComments ( "Sheet2" )
assert . NoError ( t , err )
assert . Len ( t , comments , 1 )
assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestAddComments.xlsx" ) ) )
2020-03-31 00:02:00 +08:00
f . Comments [ "xl/comments2.xml" ] = nil
2022-03-05 14:48:34 +08:00
f . Pkg . Store ( "xl/comments2.xml" , [ ] byte ( xml . Header + ` <comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><authors><author>Excelize: </author></authors><commentList><comment ref="B7" authorId="0"><text><t>Excelize: </t></text></comment></commentList></comments> ` ) )
2023-03-25 13:30:13 +08:00
comments , err = f . GetComments ( "Sheet1" )
2022-11-12 00:02:11 +08:00
assert . NoError ( t , err )
2023-03-25 13:30:13 +08:00
assert . Len ( t , comments , 2 )
comments , err = f . GetComments ( "Sheet2" )
2022-11-12 00:02:11 +08:00
assert . NoError ( t , err )
2023-03-25 13:30:13 +08:00
assert . Len ( t , comments , 1 )
comments , err = NewFile ( ) . GetComments ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , comments , 0 )
2022-11-12 00:02:11 +08:00
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test add comments with invalid sheet name
2023-11-11 00:04:05 +08:00
assert . Equal ( t , ErrSheetNameInvalid , f . AddComment ( "Sheet:1" , Comment { Cell : "A1" , Author : "Excelize" , Text : "This is a comment." } ) )
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test add comments with unsupported charset
2022-11-12 00:02:11 +08:00
f . Comments [ "xl/comments2.xml" ] = nil
f . Pkg . Store ( "xl/comments2.xml" , MacintoshCyrillicCharset )
2023-03-25 13:30:13 +08:00
_ , err = f . GetComments ( "Sheet2" )
2022-11-12 00:02:11 +08:00
assert . EqualError ( t , err , "XML syntax error on line 1: invalid UTF-8" )
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test add comments with unsupported charset
2022-11-12 00:02:11 +08:00
f . Comments [ "xl/comments2.xml" ] = nil
f . Pkg . Store ( "xl/comments2.xml" , MacintoshCyrillicCharset )
assert . EqualError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "A30" , Text : "Comment" } ) , "XML syntax error on line 1: invalid UTF-8" )
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test add comments with unsupported charset style sheet
2022-11-12 00:02:11 +08:00
f . Styles = nil
f . Pkg . Store ( defaultXMLPathStyles , MacintoshCyrillicCharset )
assert . EqualError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "A30" , Text : "Comment" } ) , "XML syntax error on line 1: invalid UTF-8" )
2023-03-25 13:30:13 +08:00
// Test get comments on not exists worksheet
comments , err = f . GetComments ( "SheetN" )
assert . Len ( t , comments , 0 )
assert . EqualError ( t , err , "sheet SheetN does not exist" )
2019-12-22 00:02:09 +08:00
}
2022-08-19 23:24:13 +08:00
func TestDeleteComment ( t * testing . T ) {
f , err := prepareTestBook1 ( )
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
2022-11-02 08:42:00 +08:00
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "A40" , Text : "Excelize: This is a comment1." } ) )
2023-07-11 23:43:45 +08:00
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "A41" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment2." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C41" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment3." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C41" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment3-1." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C42" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment4." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C41" , Paragraph : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment2." } } } ) )
2022-08-19 23:24:13 +08:00
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "A40" ) )
2023-03-25 13:30:13 +08:00
comments , err := f . GetComments ( "Sheet2" )
2022-11-12 00:02:11 +08:00
assert . NoError ( t , err )
2023-03-25 13:30:13 +08:00
assert . Len ( t , comments , 5 )
2022-11-12 00:02:11 +08:00
2023-03-25 13:30:13 +08:00
comments , err = NewFile ( ) . GetComments ( "Sheet1" )
2022-11-12 00:02:11 +08:00
assert . NoError ( t , err )
2023-03-25 13:30:13 +08:00
assert . Len ( t , comments , 0 )
2022-08-19 23:24:13 +08:00
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test delete comment with invalid sheet name
2023-11-11 00:04:05 +08:00
assert . Equal ( t , ErrSheetNameInvalid , f . DeleteComment ( "Sheet:1" , "A1" ) )
2022-08-19 23:24:13 +08:00
// Test delete all comments in a worksheet
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "A41" ) )
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "C41" ) )
2022-09-10 13:05:34 +08:00
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "C42" ) )
2023-03-25 13:30:13 +08:00
comments , err = f . GetComments ( "Sheet2" )
2022-11-12 00:02:11 +08:00
assert . NoError ( t , err )
2023-03-25 13:30:13 +08:00
assert . EqualValues ( t , 0 , len ( comments ) )
2022-08-19 23:24:13 +08:00
// Test delete comment on not exists worksheet
2022-08-28 00:16:41 +08:00
assert . EqualError ( t , f . DeleteComment ( "SheetN" , "A1" ) , "sheet SheetN does not exist" )
2022-08-19 23:24:13 +08:00
// Test delete comment with worksheet part
f . Pkg . Delete ( "xl/worksheets/sheet1.xml" )
assert . NoError ( t , f . DeleteComment ( "Sheet1" , "A22" ) )
2022-11-12 00:02:11 +08:00
f . Comments [ "xl/comments2.xml" ] = nil
f . Pkg . Store ( "xl/comments2.xml" , MacintoshCyrillicCharset )
assert . EqualError ( t , f . DeleteComment ( "Sheet2" , "A41" ) , "XML syntax error on line 1: invalid UTF-8" )
2024-01-24 14:01:56 +08:00
f = NewFile ( )
// Test delete comment on a no comments worksheet
assert . NoError ( t , f . DeleteComment ( "Sheet1" , "A1" ) )
2022-08-19 23:24:13 +08:00
}
2019-12-22 00:02:09 +08:00
func TestDecodeVMLDrawingReader ( t * testing . T ) {
f := NewFile ( )
path := "xl/drawings/vmlDrawing1.xml"
2021-07-05 00:03:56 +08:00
f . Pkg . Store ( path , MacintoshCyrillicCharset )
2022-11-13 00:40:04 +08:00
_ , err := f . decodeVMLDrawingReader ( path )
assert . EqualError ( t , err , "XML syntax error on line 1: invalid UTF-8" )
2019-12-22 00:02:09 +08:00
}
func TestCommentsReader ( t * testing . T ) {
f := NewFile ( )
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
// Test read comments with unsupported charset
2019-12-22 00:02:09 +08:00
path := "xl/comments1.xml"
2021-07-05 00:03:56 +08:00
f . Pkg . Store ( path , MacintoshCyrillicCharset )
2022-11-12 00:02:11 +08:00
_ , err := f . commentsReader ( path )
assert . EqualError ( t , err , "XML syntax error on line 1: invalid UTF-8" )
2019-12-22 00:02:09 +08:00
}
func TestCountComments ( t * testing . T ) {
f := NewFile ( )
f . Comments [ "xl/comments1.xml" ] = nil
assert . Equal ( t , f . countComments ( ) , 1 )
}
2023-07-11 23:43:45 +08:00
func TestAddDrawingVML ( t * testing . T ) {
// Test addDrawingVML with illegal cell reference
f := NewFile ( )
2023-07-21 00:03:37 +08:00
assert . Equal ( t , f . addDrawingVML ( 0 , "" , & vmlOptions { FormControl : FormControl { Cell : "*" } } ) , newCellNameToCoordinatesError ( "*" , newInvalidCellNameError ( "*" ) ) )
2023-07-11 23:43:45 +08:00
f . Pkg . Store ( "xl/drawings/vmlDrawing1.vml" , MacintoshCyrillicCharset )
2023-10-11 00:04:38 +08:00
assert . EqualError ( t , f . addDrawingVML ( 0 , "xl/drawings/vmlDrawing1.vml" , & vmlOptions { sheet : "Sheet1" , FormControl : FormControl { Cell : "A1" } } ) , "XML syntax error on line 1: invalid UTF-8" )
2023-07-11 23:43:45 +08:00
}
2023-07-13 00:03:24 +08:00
func TestFormControl ( t * testing . T ) {
2023-07-11 23:43:45 +08:00
f := NewFile ( )
2023-07-28 00:24:08 +08:00
formControls := [ ] FormControl {
{
Cell : "D1" , Type : FormControlButton , Macro : "Button1_Click" ,
} ,
{
Cell : "A1" , Type : FormControlButton , Macro : "Button1_Click" ,
2023-07-31 00:08:10 +08:00
Width : 140 , Height : 60 , Text : "Button 1\n" ,
2023-07-28 00:24:08 +08:00
Paragraph : [ ] RichTextRun {
{
Font : & Font {
Bold : true ,
Italic : true ,
Underline : "single" ,
Family : "Times New Roman" ,
Size : 14 ,
Color : "777777" ,
} ,
Text : "C1=A1+B1" ,
2023-07-11 23:43:45 +08:00
} ,
} ,
2023-07-28 00:24:08 +08:00
Format : GraphicOptions { PrintObject : boolPtr ( true ) , Positioning : "absolute" } ,
2023-07-11 23:43:45 +08:00
} ,
2023-07-28 00:24:08 +08:00
{
Cell : "A5" , Type : FormControlCheckBox , Text : "Check Box 1" ,
Checked : true , Format : GraphicOptions {
PrintObject : boolPtr ( false ) , Positioning : "oneCell" ,
} ,
2023-07-21 00:03:37 +08:00
} ,
2023-07-28 00:24:08 +08:00
{
Cell : "A6" , Type : FormControlCheckBox , Text : "Check Box 2" ,
Format : GraphicOptions { Positioning : "twoCell" } ,
} ,
{
Cell : "A7" , Type : FormControlOptionButton , Text : "Option Button 1" , Checked : true ,
} ,
{
Cell : "A8" , Type : FormControlOptionButton , Text : "Option Button 2" ,
} ,
{
Cell : "D3" , Type : FormControlGroupBox , Text : "Group Box 1" ,
Width : 140 , Height : 60 ,
} ,
{
Cell : "A9" , Type : FormControlLabel , Text : "Label 1" , Width : 140 ,
} ,
{
Cell : "C5" , Type : FormControlSpinButton , Width : 40 , Height : 60 ,
CurrentVal : 7 , MinVal : 5 , MaxVal : 10 , IncChange : 1 , CellLink : "C2" ,
} ,
{
Cell : "D7" , Type : FormControlScrollBar , Width : 140 , Height : 20 ,
CurrentVal : 50 , MinVal : 10 , MaxVal : 100 , IncChange : 1 , PageChange : 1 , Horizontally : true , CellLink : "C3" ,
} ,
{
Cell : "G1" , Type : FormControlScrollBar , Width : 20 , Height : 140 ,
CurrentVal : 50 , MinVal : 1000 , MaxVal : 100 , IncChange : 1 , PageChange : 1 , CellLink : "C4" ,
} ,
}
for _ , formCtrl := range formControls {
assert . NoError ( t , f . AddFormControl ( "Sheet1" , formCtrl ) )
}
// Test get from controls
result , err := f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , result , 11 )
for i , formCtrl := range formControls {
assert . Equal ( t , formCtrl . Type , result [ i ] . Type )
assert . Equal ( t , formCtrl . Cell , result [ i ] . Cell )
assert . Equal ( t , formCtrl . Macro , result [ i ] . Macro )
assert . Equal ( t , formCtrl . Checked , result [ i ] . Checked )
assert . Equal ( t , formCtrl . CurrentVal , result [ i ] . CurrentVal )
assert . Equal ( t , formCtrl . MinVal , result [ i ] . MinVal )
assert . Equal ( t , formCtrl . MaxVal , result [ i ] . MaxVal )
assert . Equal ( t , formCtrl . IncChange , result [ i ] . IncChange )
assert . Equal ( t , formCtrl . Horizontally , result [ i ] . Horizontally )
assert . Equal ( t , formCtrl . CellLink , result [ i ] . CellLink )
2023-07-31 00:08:10 +08:00
assert . Equal ( t , formCtrl . Text , result [ i ] . Text )
assert . Equal ( t , len ( formCtrl . Paragraph ) , len ( result [ i ] . Paragraph ) )
2023-07-28 00:24:08 +08:00
}
2023-07-11 23:43:45 +08:00
assert . NoError ( t , f . SetSheetProps ( "Sheet1" , & SheetPropsOptions { CodeName : stringPtr ( "Sheet1" ) } ) )
file , err := os . ReadFile ( filepath . Join ( "test" , "vbaProject.bin" ) )
assert . NoError ( t , err )
assert . NoError ( t , f . AddVBAProject ( file ) )
assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestAddFormControl.xlsm" ) ) )
assert . NoError ( t , f . Close ( ) )
f , err = OpenFile ( filepath . Join ( "test" , "TestAddFormControl.xlsm" ) )
assert . NoError ( t , err )
2023-07-28 00:24:08 +08:00
// Test get from controls before add form controls
result , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , result , 11 )
// Test add from control to a worksheet which already contains form controls
2023-07-11 23:43:45 +08:00
assert . NoError ( t , f . AddFormControl ( "Sheet1" , FormControl {
2023-07-31 00:08:10 +08:00
Cell : "D4" , Type : FormControlButton , Macro : "Button1_Click" ,
Paragraph : [ ] RichTextRun { { Font : & Font { Underline : "double" } , Text : "Button 2" } } ,
2023-07-11 23:43:45 +08:00
} ) )
2023-07-28 00:24:08 +08:00
// Test get from controls after add form controls
result , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , result , 12 )
2023-07-11 23:43:45 +08:00
// Test add unsupported form control
assert . Equal ( t , f . AddFormControl ( "Sheet1" , FormControl {
2023-07-21 00:03:37 +08:00
Cell : "A1" , Type : 0x37 , Macro : "Button1_Click" ,
2023-07-11 23:43:45 +08:00
} ) , ErrParameterInvalid )
// Test add form control on not exists worksheet
2023-09-28 08:53:54 +08:00
assert . Equal ( t , ErrSheetNotExist { "SheetN" } , f . AddFormControl ( "SheetN" , FormControl {
2023-07-21 00:03:37 +08:00
Cell : "A1" , Type : FormControlButton , Macro : "Button1_Click" ,
2023-09-28 08:53:54 +08:00
} ) )
2023-07-21 00:03:37 +08:00
// Test add form control with invalid positioning types
assert . Equal ( t , f . AddFormControl ( "Sheet1" , FormControl {
Cell : "A1" , Type : FormControlButton ,
Format : GraphicOptions { Positioning : "x" } ,
} ) , ErrParameterInvalid )
// Test add spin form control with illegal cell link reference
assert . Equal ( t , f . AddFormControl ( "Sheet1" , FormControl {
Cell : "C5" , Type : FormControlSpinButton , CellLink : "*" ,
} ) , newCellNameToCoordinatesError ( "*" , newInvalidCellNameError ( "*" ) ) )
// Test add spin form control with invalid scroll value
assert . Equal ( t , f . AddFormControl ( "Sheet1" , FormControl {
Cell : "C5" , Type : FormControlSpinButton , CurrentVal : MaxFormControlValue + 1 ,
2023-09-28 08:53:54 +08:00
} ) , ErrFormControlValue )
2023-07-11 23:43:45 +08:00
assert . NoError ( t , f . Close ( ) )
2023-07-13 00:03:24 +08:00
// Test delete form control
f , err = OpenFile ( filepath . Join ( "test" , "TestAddFormControl.xlsm" ) )
assert . NoError ( t , err )
assert . NoError ( t , f . DeleteFormControl ( "Sheet1" , "D1" ) )
assert . NoError ( t , f . DeleteFormControl ( "Sheet1" , "A1" ) )
2023-07-28 00:24:08 +08:00
// Test get from controls after delete form controls
result , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , result , 9 )
2023-07-13 00:03:24 +08:00
// Test delete form control on not exists worksheet
2023-09-28 08:53:54 +08:00
assert . Equal ( t , ErrSheetNotExist { "SheetN" } , f . DeleteFormControl ( "SheetN" , "A1" ) )
2023-07-28 00:24:08 +08:00
// Test delete form control with illegal cell link reference
2023-07-13 00:03:24 +08:00
assert . Equal ( t , f . DeleteFormControl ( "Sheet1" , "A" ) , newCellNameToCoordinatesError ( "A" , newInvalidCellNameError ( "A" ) ) )
assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestDeleteFormControl.xlsm" ) ) )
assert . NoError ( t , f . Close ( ) )
// Test delete form control with expected element
f , err = OpenFile ( filepath . Join ( "test" , "TestAddFormControl.xlsm" ) )
assert . NoError ( t , err )
f . Pkg . Store ( "xl/drawings/vmlDrawing1.vml" , MacintoshCyrillicCharset )
assert . Error ( t , f . DeleteFormControl ( "Sheet1" , "A1" ) , "XML syntax error on line 1: invalid UTF-8" )
2023-07-31 00:08:10 +08:00
// Test delete form controls with invalid shape anchor
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : "<x:ClientData ObjectType=\"Scroll\"><x:Anchor>0</x:Anchor></x:ClientData>" } } ,
}
assert . Equal ( t , ErrParameterInvalid , f . DeleteFormControl ( "Sheet1" , "A1" ) )
2023-07-13 00:03:24 +08:00
assert . NoError ( t , f . Close ( ) )
// Test delete form control on a worksheet without form control
f = NewFile ( )
assert . NoError ( t , f . DeleteFormControl ( "Sheet1" , "A1" ) )
2023-07-28 00:24:08 +08:00
// Test get form controls on a worksheet without form control
_ , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
// Test get form controls on not exists worksheet
_ , err = f . GetFormControls ( "SheetN" )
2023-09-28 08:53:54 +08:00
assert . Equal ( t , ErrSheetNotExist { "SheetN" } , err )
2023-07-28 00:24:08 +08:00
// Test get form controls with unsupported charset VML drawing
f , err = OpenFile ( filepath . Join ( "test" , "TestAddFormControl.xlsm" ) )
assert . NoError ( t , err )
f . Pkg . Store ( "xl/drawings/vmlDrawing1.vml" , MacintoshCyrillicCharset )
_ , err = f . GetFormControls ( "Sheet1" )
assert . EqualError ( t , err , "XML syntax error on line 1: invalid UTF-8" )
// Test get form controls with unsupported shape type
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "_x0000_t202" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , formControls , 0 )
2023-07-31 00:08:10 +08:00
// Test get form controls with bold font format
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : "<v:textbox><div><font><b>Text</b></font></div></v:textbox><x:ClientData ObjectType=\"Scroll\"><x:Anchor>0,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . True ( t , formControls [ 0 ] . Paragraph [ 0 ] . Font . Bold )
// Test get form controls with italic font format
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : "<v:textbox><div><font><i>Text</i></font></div></v:textbox><x:ClientData ObjectType=\"Scroll\"><x:Anchor>0,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . True ( t , formControls [ 0 ] . Paragraph [ 0 ] . Font . Italic )
// Test get form controls with font format
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : "<v:textbox><div><font face=\"Calibri\" size=\"280\" color=\"#777777\">Text</font></div></v:textbox><x:ClientData ObjectType=\"Scroll\"><x:Anchor>0,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Equal ( t , "Calibri" , formControls [ 0 ] . Paragraph [ 0 ] . Font . Family )
assert . Equal ( t , 14.0 , formControls [ 0 ] . Paragraph [ 0 ] . Font . Size )
assert . Equal ( t , "#777777" , formControls [ 0 ] . Paragraph [ 0 ] . Font . Color )
// Test get form controls with italic font format
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : "<v:textbox><div><font><i>Text</i></font></div></v:textbox><x:ClientData ObjectType=\"Scroll\"><x:Anchor>0,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . True ( t , formControls [ 0 ] . Paragraph [ 0 ] . Font . Italic )
2023-07-28 00:24:08 +08:00
// Test get form controls with invalid column number
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
2023-07-31 00:08:10 +08:00
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : fmt . Sprintf ( "<x:ClientData ObjectType=\"Scroll\"><x:Anchor>%d,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" , MaxColumns ) } } ,
2023-07-28 00:24:08 +08:00
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . Equal ( t , err , ErrColumnNumber )
assert . Len ( t , formControls , 0 )
// Test get form controls with comment (Note) shape type
f . DecodeVMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & decodeVmlDrawing {
Shape : [ ] decodeShape { { Type : "#_x0000_t201" , Val : "<x:ClientData ObjectType=\"Note\"></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , formControls , 0 )
// Test get form controls with unsupported shape type
f . VMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & vmlDrawing {
Shape : [ ] xlsxShape { { Type : "_x0000_t202" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , formControls , 0 )
// Test get form controls with invalid column number
f . VMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & vmlDrawing {
2023-07-31 00:08:10 +08:00
Shape : [ ] xlsxShape { { Type : "#_x0000_t201" , Val : fmt . Sprintf ( "<x:ClientData ObjectType=\"Scroll\"><x:Anchor>%d,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" , MaxColumns ) } } ,
2023-07-28 00:24:08 +08:00
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . Equal ( t , err , ErrColumnNumber )
assert . Len ( t , formControls , 0 )
2023-07-31 00:08:10 +08:00
// Test get form controls with invalid shape anchor
f . VMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & vmlDrawing {
Shape : [ ] xlsxShape { { Type : "#_x0000_t201" , Val : "<x:ClientData ObjectType=\"Scroll\"><x:Anchor>x,0,0,0,0,0,0,0</x:Anchor></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . Equal ( t , ErrColumnNumber , err )
assert . Len ( t , formControls , 0 )
2023-07-28 00:24:08 +08:00
// Test get form controls with comment (Note) shape type
f . VMLDrawing [ "xl/drawings/vmlDrawing1.vml" ] = & vmlDrawing {
Shape : [ ] xlsxShape { { Type : "#_x0000_t201" , Val : "<x:ClientData ObjectType=\"Note\"></x:ClientData>" } } ,
}
formControls , err = f . GetFormControls ( "Sheet1" )
assert . NoError ( t , err )
assert . Len ( t , formControls , 0 )
assert . NoError ( t , f . Close ( ) )
}
func TestExtractFormControl ( t * testing . T ) {
// Test extract form control with unsupported charset
_ , err := extractFormControl ( string ( MacintoshCyrillicCharset ) )
assert . EqualError ( t , err , "XML syntax error on line 1: invalid UTF-8" )
2023-07-11 23:43:45 +08:00
}