Skip to content

Commit

Permalink
Fix validate examples (#54)
Browse files Browse the repository at this point in the history
* fix validate-example.sh

* rename ci title

* add ci build-examples.sh

* update ci.yml path

* update path for run main.go

* update path

* update examples

* update validate-examples.sh

* update

* update usage-logs docx

* add go mod tidy

* remove node-remove example

* remove document comment example

* remove mutiple chart from csv

* remove line chart from csv example

* remove comment-remove example

* remove comment-list example

* remove copy reorder slide

* fix examples

* add variable for commentId on comment-remove example

* add presentation use template sales example
  • Loading branch information
sampila authored Feb 13, 2025
1 parent 8d2657b commit 9600d98
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 54 deletions.
16 changes: 16 additions & 0 deletions .ci/build-examples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

cd $PWD
go get github.com/go-ole/go-ole/oleutil@v1.3.0
go mod tidy

:> build_errors
find $PWD -maxdepth 2 -mindepth 1 -not -path '*/\.*' -type d -exec sh -c "cd {}; echo building {}; go build -tags=test main.go" 2>>build_log \;
grep -v "^go: " build_log | grep -v main.go > build_errors
if [[ $(wc -l build_errors | awk '{print $1}') == "0" ]]; then
exit 0
fi
echo BUILD ERRORS:
cat build_errors
echo END OF BUILD ERRORS.
exit 1
8 changes: 4 additions & 4 deletions .ci/validate-examples.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash
:> errors
output=$(find ../_examples -maxdepth 2 -mindepth 2 ! -name "metered" ! -name "offline" ! -name "metered-non-persistent-cache" ! -name "usage-logs" -print0 | xargs -0 -I{} sh -c "cd {}; echo running {}; ./main")
output=$(find .. -maxdepth 2 -mindepth 2 -not -path '*/\.*' -type d ! -name "metered" ! -name "offline" ! -name "metered-non-persistent-cache" ! -name "usage-logs" -print0 | xargs -0 -I{} sh -c "cd {}; echo running {}; ./main")
if [ $? -ne 0 ]; then
echo $output >> errors
fi
export PATH=$PATH:$HOME/dotnet
find ../_examples -name "*.docx" $(printf "! -name %s " $(cat skip_files)) -exec ./dotnet_run.sh {} docx \;
find ../_examples -name "*.xlsx" $(printf "! -name %s " $(cat skip_files)) -exec ./dotnet_run.sh {} xlsx \;
find ../_examples -name "*.pptx" $(printf "! -name %s " $(cat skip_files)) -exec ./dotnet_run.sh {} pptx \;
find .. -name "*.docx" $(printf "! -name %s " $(cat skip_files)) -exec ./dotnet_run.sh {} docx \;
find .. -name "*.xlsx" $(printf "! -name %s " $(cat skip_files)) -exec ./dotnet_run.sh {} xlsx \;
find .. -name "*.pptx" $(printf "! -name %s " $(cat skip_files)) -exec ./dotnet_run.sh {} pptx \;
echo Errors: $(wc -l errors)
if [[ $(wc -l errors | awk '{print $1}') == "0" ]]; then
exit 0
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: UniOffice CI
name: UniOffice Examples CI

on:
push:
Expand Down Expand Up @@ -34,7 +34,7 @@ jobs:
wget https://download.visualstudio.microsoft.com/download/pr/4e3b04aa-c015-4e06-a42e-05f9f3c54ed2/74d1bb68e330eea13ecfc47f7cf9aeb7/dotnet-sdk-8.0.404-linux-x64.tar.gz
tar -xf dotnet-sdk-8.0.404-linux-x64.tar.gz
- name: test
- name: Test and validate examples
run: |
export REPO=$PWD
cd $REPO/.ci
Expand All @@ -44,7 +44,7 @@ jobs:
cd $REPO/.ci/openxml-validator
$HOME/dotnet/dotnet build --configuration Release
cd $REPO
./build_examples.sh
./.ci/build-examples.sh
cd $REPO/.ci
./validate-examples.sh
if [ $? -ne 0 ]
Expand Down
9 changes: 1 addition & 8 deletions document/comment-list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ func init() {
}

func main() {
if len(os.Args) < 2 {
fmt.Printf("Syntax: go run main.go input.docx\n")
os.Exit(1)
}

inputPath := os.Args[1]

doc, err := document.Open(inputPath)
doc, err := document.Open("sample.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
Expand Down
Binary file added document/comment-list/sample.docx
Binary file not shown.
19 changes: 4 additions & 15 deletions document/comment-remove/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"log"
"os"
"strconv"

"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
Expand All @@ -26,28 +25,18 @@ func init() {
}

func main() {
if len(os.Args) < 3 {
fmt.Printf("Syntax: go run main.go input.docx comment_id\n")
os.Exit(1)
}

inputPath := os.Args[1]
commentId, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalf("error parsing comment id: %s", err)
}

doc, err := document.Open(inputPath)
doc, err := document.Open("sample.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()

listComments(doc)

if sucess := doc.RemoveComment(int64(commentId)); !sucess {
fmt.Println("Failed removing comment")
commentId := int64(2)

if ok := doc.RemoveComment(commentId); !ok {
fmt.Println("Failed removing comment")
return
}

Expand Down
Binary file added document/comment-remove/sample.docx
Binary file not shown.
14 changes: 2 additions & 12 deletions document/node-remove/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package main

import (
"fmt"
"log"
"os"

Expand All @@ -20,16 +19,7 @@ func init() {
}

func main() {
args := os.Args
if len(args) < 2 {
fmt.Printf("Usage %s go run main.go input.docx output.docx", os.Args[0])
return
}

inputPath := args[1]
outputPath := args[2]

doc, err := document.Open(inputPath)
doc, err := document.Open("sample.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
Expand All @@ -46,7 +36,7 @@ func main() {
}
}

err = doc.SaveToFile(outputPath)
err = doc.SaveToFile("output.docx")
if err != nil {
panic(err)
}
Expand Down
Binary file modified document/template-with-header/letter_template.docx
Binary file not shown.
Binary file added document/use-template/use-template.docx
Binary file not shown.
Binary file modified license/usage-logs/grocery_list.docx
Binary file not shown.
Binary file modified license/usage-logs/updated_list.docx
Binary file not shown.
2 changes: 1 addition & 1 deletion presentation/copy-reorder-slide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
}

// move inserted slide to the very beginning of presentation, it becomes slide 0
err = pptTo.MoveSlide(4, 0)
err = pptTo.MoveSlide(len(pptTo.Slides())-1, 1)
if err != nil {
fmt.Println("error moving slide ", err)
os.Exit(1)
Expand Down
157 changes: 157 additions & 0 deletions presentation/use-template-sales/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright 2025 FoxyUtils ehf. All rights reserved.
package main

import (
"fmt"
"log"
"os"

"encoding/json"

"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/schema/soo/pml"

"github.com/unidoc/unioffice/v2/presentation"
)

type SaleInfo struct {
Area string
Sale string
Customers int
Manager string
}

type Data struct {
Year int
ID string
SaleData []SaleInfo
}

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}

func main() {
jsonData := []byte(`
{
"year":2020,
"id":"JI23SA",
"SaleData":[
{
"area": "Michigan",
"sale": "10 million",
"customers": 10000,
"manager": "Henry"
},
{
"area": "Cincinnati",
"sale": "2 million",
"customers": 490,
"manager": "John Green"
},
{
"area": "Washington",
"sale": "150 million",
"customers": 100000,
"manager": "Smith Johnson"
}
]
}
`)

ppt, err := presentation.OpenTemplate("template.pptx")
if err != nil {
log.Fatalf("unable to open template: %s", err)
}
defer ppt.Close()

for i, layout := range ppt.SlideLayouts() {
fmt.Println(i, " LL ", layout.Name(), "/", layout.Type())
}

var res Data
err = json.Unmarshal(jsonData, &res)
if err != nil {
log.Fatalf("error unmarshalling JSON: %s", err)
}
saleData := res.SaleData

for _, data := range saleData { // Iterate through the sale data
// Remove any existing slides
for _, s := range ppt.Slides() {
err := ppt.RemoveSlide(s)
if err != nil {
log.Fatalf("error removing slide: %s", err)
}
}

l, err := ppt.GetLayoutByName("Title and Caption")
if err != nil {
log.Fatalf("error retrieving layout: %s", err)
}

sld, err := ppt.AddDefaultSlideWithLayout(l)
if err != nil {
log.Fatalf("error adding slide: %s", err)
}

ph, err := sld.GetPlaceholder(pml.ST_PlaceholderTypeTitle)
if err != nil {
log.Fatalf("error getting placeholder type title: %s", err)
}

ph.SetText(fmt.Sprintf("Sale Data For: %s", data.Area))

ph, err = sld.GetPlaceholder(pml.ST_PlaceholderTypeBody)
if err != nil {
log.Fatalf("error getting placeholder type body: %s", err)
}

ph.SetText("Created with github.com/unidoc/unioffice/")

tac, err := ppt.GetLayoutByName("Title and Content")
if err != nil {
log.Fatalf("error retrieving layout: %s", err)
}

sld, err = ppt.AddDefaultSlideWithLayout(tac)
if err != nil {
log.Fatalf("error adding slide: %s", err)
}

ph, err = sld.GetPlaceholder(pml.ST_PlaceholderTypeTitle)
if err != nil {
log.Fatalf("error getting placeholder type title: %s", err)
}

ph.SetText(fmt.Sprintf("Data for %s, Managed by %s", data.Area, data.Manager))

ph, err = sld.GetPlaceholderByIndex(1)
if err != nil {
log.Fatalf("error getting placeholder by index: %s", err)
}

ph.ClearAll()

para := ph.AddParagraph()
run := para.AddRun()
run.SetText(fmt.Sprintf("Here is the number of sales in %s: $%s", data.Area, data.Sale))

para = ph.AddParagraph()
run = para.AddRun()
run.SetText(fmt.Sprintf("Number of Customers: %d", data.Customers))

para = ph.AddParagraph()
run = para.AddRun()
run.SetText(fmt.Sprintf("Manager: %s", data.Manager))

if err := ppt.SaveToFile(fmt.Sprintf("%s.pptx", data.Area)); err != nil {
log.Fatalf("error saving presentation: %s", err)
}
}
}
Binary file added presentation/use-template-sales/template.pptx
Binary file not shown.
13 changes: 2 additions & 11 deletions spreadsheet/line-chart-from-csv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ func init() {
}

func main() {
args := os.Args
if len(args) < 2 {
fmt.Printf("Usage %s go run main.go input.csv output.xlsx", os.Args[0])
return
}

inputPath := args[1]
outputPath := args[2]

sliceData, err := readCsv(inputPath)
sliceData, err := readCsv("example-data.csv")
if err != nil {
log.Fatalf("error reading csv: %s", err)
}
Expand Down Expand Up @@ -99,7 +90,7 @@ func main() {
log.Fatalf("error validating sheet: %s", err)
}

if err := ss.SaveToFile(outputPath); err != nil {
if err := ss.SaveToFile("output.xlsx"); err != nil {
log.Fatalf("error saving: %s", err)
}
}
Expand Down
Binary file modified spreadsheet/line-chart-from-csv/output.xlsx
Binary file not shown.
Binary file modified spreadsheet/multiple-charts-from-csv/charts-from-csv.xlsx
Binary file not shown.

0 comments on commit 9600d98

Please sign in to comment.