Просмотр исходного кода

Merge pull request #246 from nad2000/retrieve-comments

added retrieval of worksheet comments
xuri 7 лет назад
Родитель
Сommit
58a7b23d11
2 измененных файлов с 22 добавлено и 0 удалено
  1. 17 0
      comment.go
  2. 5 0
      excelize_test.go

+ 17 - 0
comment.go

@@ -19,6 +19,23 @@ func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
 	return &format, err
 	return &format, err
 }
 }
 
 
+// GetComments retrievs all comments and returns a map
+// of worksheet name to the worksheet comments.
+func (f *File) GetComments() (comments map[string]*xlsxComments) {
+	comments = map[string]*xlsxComments{}
+	for n := range f.sheetMap {
+		commentID := f.GetSheetIndex(n)
+		commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
+		c, ok := f.XLSX[commentsXML]
+		if ok {
+			d := xlsxComments{}
+			xml.Unmarshal([]byte(c), &d)
+			comments[n] = &d
+		}
+	}
+	return
+}
+
 // AddComment provides the method to add comment in a sheet by given worksheet
 // AddComment provides the method to add comment in a sheet by given worksheet
 // index, cell and format set (such as author and text). Note that the max
 // index, cell and format set (such as author and text). Note that the max
 // author length is 255 and the max text length is 32512. For example, add a
 // author length is 255 and the max text length is 32512. For example, add a

+ 5 - 0
excelize_test.go

@@ -817,6 +817,11 @@ func TestAddComments(t *testing.T) {
 	if err != nil {
 	if err != nil {
 		t.Error(err)
 		t.Error(err)
 	}
 	}
+	allComments := xlsx.GetComments()
+	if len(allComments) != 2 {
+		t.Error("Expected 2 comment entry elements.")
+	}
+
 }
 }
 
 
 func TestAutoFilter(t *testing.T) {
 func TestAutoFilter(t *testing.T) {