Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用golang library unioffice在docx中填写表单字段_Go_Office365 - Fatal编程技术网

使用golang library unioffice在docx中填写表单字段

使用golang library unioffice在docx中填写表单字段,go,office365,Go,Office365,我正在尝试使用unioffice library填写表单字段。我正在处理的文档包含几个段落。段落包含多个表单字段。 我想填写文档中的所有表单字段。下面是我正在运行的代码: doc, err := document.Open("form.docx") if err != nil { log.Fatalf("error opening form: %s", err) } for i := range doc.FormFields() {

我正在尝试使用unioffice library填写表单字段。我正在处理的文档包含几个段落。段落包含多个表单字段。 我想填写文档中的所有表单字段。下面是我正在运行的代码:

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

    for i := range doc.FormFields() {
        doc.FormFields()[i].SetValue("test")
    }

    doc.SaveToFile("filled-form.docx")

但是,并不是所有的表单字段都已填写。

在我看来就像
func(d*Document)Save(w io.Writer)error{}
中的一个bug。我可以读写每个
FormFields
,但只有段落中最后一个
FormField
值实际保存到文件中

在保存到文件之前,以下代码的工作方式与预期相同。(这意味着它会打印出以前设置的值)。我看到您已经在github()上发行了一期新的内容,希望您在这方面有更多的运气

package main

import (
    "github.com/unidoc/unioffice/document"
    "io/ioutil"
    "log"
    "os"
)

func main() {
    _, err := ioutil.ReadFile("filled-form.docx")
    if err == nil {
        err = os.Remove("filled-form.docx")
        if err != nil {
            log.Fatal(err)
        }
    }

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

    for _, f := range doc.FormFields() {
        if f.Type() == document.FormFieldType(1) {
            f.SetValue("test")
        }
    }

    for _, f := range doc.FormFields() {
        log.Println("-------------------")
        log.Println(f.Name())
        log.Println(f.Value())
    }

    err = doc.SaveToFile("filled-form.docx")
    if err != nil {
        log.Fatal(err)
    }
}


你能举个word文件的例子吗?此外,使用
for ux,file:=range doc.FormFields()
代替
i
@sHartmann,文件如下: