【问题标题】:PhpWord TemplateProcessor clone table rows and insert information in themPhpWord TemplateProcessor 克隆表行并在其中插入信息
【发布时间】:2020-05-08 11:11:16
【问题描述】:

我对 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"
    ]
  ]
}

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

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"
]

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

如果您能告诉我如何克隆这一行并在其中插入值,我将不胜感激。

【问题讨论】:

    标签: laravel invoice phpword phpoffice


    【解决方案1】:

    问题解决了。我做了这样的表结构

    +-----------+----------------+
    | ${row}    | ${Item}        |
    |           |                +
    |           | ${ItemInfo}    |
    +-----------+----------------+
    +-----------+----------------+
    | ${row#1}  | ${Item}        |
    |           |                +
    |           | ${ItemInfo}    |
    +-----------+----------------+
    

    我正在使用cloneRow('ROW', 2) 方法克隆该行 PhpWord Docs 这给了我 2 个 ROW 副本,我可以使用它们并添加到每个副本 #INDEX。这样,我遍历它们并将 place holder 替换为像这样的实际值

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

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

    【讨论】:

      猜你喜欢
      • 2018-10-10
      • 1970-01-01
      • 2012-06-28
      • 2021-10-31
      • 2014-08-23
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多