PhpWord TemplateProcessor 克隆表格行并在其中插入信息

Kri*_*lev 3 invoice laravel phpword phpoffice

我有 phpword 表的问题。

表结构

我有下表,我想克隆第一个表行并替换其中的信息。到目前为止,我没有任何进展。我使用getVariables()方法从文档中获取所有变量并循环遍历它们。我已经检查过该值是否是数组,如果是它属于该行。我按照以下方式构建了数据

Collection {#971 ?
  #items: array:12 [?
    "ticket_id" => array:1 [?
      0 => 7
      0 => 6
    ]
    "ticket_number" => array:2 [?
      0 => "157-12313121321"
      1 => null
    ]
    "price_offered_bgn" => array:2 [?
      0 => 978.0
      1 => 196.0
    ]
    "ticket_is" => array:1 [?
      0 => "Requested"
    ]
    "departure_date" => array:2 [?
      0 => "2020-10-20 00:00:00"
      1 => "2020-01-29 00:00:00"
    ]
    "return_date" => array:2 [?
      0 => "2020-10-29 00:00:00"
      1 => null
    ]
    "company_address" => array:1 [?
      0 => "ADDRESS"
    ]
    "company_bulstat" => array:1 [?
      0 => ""
    ]
    "company_dds_number" => array:1 [?
      0 => "BG 104023232353"
    ]
    "mol" => array:1 [?
      0 => "Gleichner"
    ]
    "first_name" => array:2 [?
      0 => "Araceli"
      1 => "Francisca"
    ]
    "last_name" => array:2 [?
      0 => "Gleichner"
      1 => "Schmitt"
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)

在我尝试克隆变量并插入值后,我得出了以下结果

array:4 [?
  0 => "TICKET_NUMBER"
  1 => "FIRST_NAME"
  2 => "LAST_NAME"
  3 => "DEPARTURE_DATE"
]
array:9 [?
  0 => "FIRST_NAME#1"
  1 => "LAST_NAME#1"
  2 => "DEPARTURE_DATE#1"
  3 => "RETURN_DATE#1"
  4 => "TICKET_NUMBER#2"
  5 => "FIRST_NAME#2"
  6 => "LAST_NAME#2"
  7 => "DEPARTURE_DATE#2"
  8 => "RETURN_DATE#2"
]
Run Code Online (Sandbox Code Playgroud)

而这个错误 Can not clone row, template variable not found or variable contains markup. at TemplateProcessor->cloneRow('${FIRST_NAME}', 2)

如果您对我如何克隆这一行并在其中插入值有任何想法,我将不胜感激。

Kri*_*lev 5

问题解决了。我已经制作了这样的表格结构

+-----------+----------------+
| ${row}    | ${Item}        |
|           |                +
|           | ${ItemInfo}    |
+-----------+----------------+
+-----------+----------------+
| ${row#1}  | ${Item}        |
|           |                +
|           | ${ItemInfo}    |
+-----------+----------------+
Run Code Online (Sandbox Code Playgroud)

我正在使用PhpWord DocscloneRow('ROW', 2)方法 克隆该行,这为我提供了 2 个 ROW 副本,我可以使用这些副本并添加其中的每个副本。这样我循环遍历它们并用这样的实际值替换占位符#INDEX

 foreach ($fields as $key => $value) {
$this->wordFile->setValue(strtoupper($key) . '#' . $index, $value);
$this->wordFile->setValue('ROW#' . $index, $index);
}
Run Code Online (Sandbox Code Playgroud)

KEY变量是字段名,然后我将串联#INDEX到它。 克隆的行以索引 1 开始(#1、#2、#3 等等...)。