superbig/craft3-beam

在模板中生成CSV和XLS文件

安装数量: 30,343

依赖项: 0

建议者: 0

安全: 0

星标: 18

关注者: 2

分叉: 6

公开问题: 4

类型:craft-plugin

5.0.0 2024-04-24 09:19 UTC

This package is auto-updated.

Last update: 2024-09-14 10:06:15 UTC


README

在模板中生成CSV和XLS文件

Screenshot

需求

此插件需要Craft CMS 3.0.0-beta.23或更高版本。

安装

要安装插件,请按照以下说明操作。

  1. 打开您的终端并转到您的Craft项目

     cd /path/to/project
    
  2. 然后让Composer加载插件

     composer require superbig/craft3-beam
    
  3. 在控制面板中,转到设置 → 插件,然后点击Beam的“安装”按钮。

使用Beam

使用Beam的工作起点是创建一个实例

{% set options = {
    header: ['Email', 'Name'],
    content: [
        [ 'test@example.com', 'John Doe' ],
        [ 'another+test@example.com', 'Jane Doe' ],
        [ 'third+test@example.com', 'Trond Johansen' ],
    ]
} %}
{% set beam = craft.beam.create(options) %}

这将在幕后返回一个BeamModel

如果您想动态添加内容,比如从一个循环中,您可以使用append方法

{% set myUserQuery = craft.users()
    .group('authors') %}

{# Fetch the users #}
{% set users = myUserQuery.all() %}

{# Display the list #}
{% for user in users %}
    {% do beam.append([user.username, user.name, user.email]) %}
{% endfor %}

生成CSV

{% do beam.csv() %}

生成XLSX

{% do beam.xlsx() %}

动态更改配置

设置文件的头部(第一行)

{% do beam.setHeader([ 'Username', 'Name', 'Email' ]) %}

设置文件名

{% set currentDate = now|date('Y-m-d') %}
{% do beam.setFilename("report-#{currentDate}") %}

覆盖内容

{% do beam.setContent([
    [ 'test@example.com', 'John Doe' ],
    [ 'another+test@example.com', 'Jane Doe' ],
    [ 'third+test@example.com', 'Trond Johansen' ],
]) %}

支持XLSX的单元格自定义格式化

{% set options = {
    header: ['Email', 'Name', { text: 'Number', type: 'number' }, { text: 'Date', type: 'date' }],
    content: [
        [ 'test@example.com', 'John Doe', 100000, '2022-06-10'],
        [ 'another+test@example.com', 'Jane Doe', 252323, '2022-06-22'],
        [ 'third+test@example.com', 'Trond Johansen', 30, '2022-06-22'],
        [ 'third+test@example.com', 'Trond Johansen', 6233, '2023-06-22'],
    ]
} %}
{% set beam = craft.beam.create(options) %}
{%  do beam.xlsx() %}

支持这些类型

Superbig提供