Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[63739] Add derived values as additional columns in XLS and CSV exports

Christophe Bliard838d16ae2ebd
diff --git a/app/models/work_package/exports/additional_columns.rb b/app/models/work_package/exports/additional_columns.rb
new file mode 100644
index 00000000000..d6893f2f349
--- /dev/null
+++ b/app/models/work_package/exports/additional_columns.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module WorkPackage::Exports
+ # Adds extra columns when some particular columns are present.
+ #
+ # For instance, adds a 'Total work' column when the 'Work' column is present.
+ module AdditionalColumns
+ ADDITIONAL_COLUMNS = {
+ estimated_hours: [:derived_estimated_hours],
+ remaining_hours: [:derived_remaining_hours]
+ }.freeze
+
+ def get_columns
+ super.flat_map { |column| [column] + additional_columns(column) }
+ end
+
+ def additional_columns(column)
+ ADDITIONAL_COLUMNS
+ .fetch(column.name, [])
+ .map do |additional_column_name|
+ Queries::WorkPackages::Selects::PropertySelect.new(additional_column_name)
+ end
+ end
+ end
+end
diff --git a/app/models/work_package/exports/csv.rb b/app/models/work_package/exports/csv.rb
index a16a5309f1e..03acf053380 100644
--- a/app/models/work_package/exports/csv.rb
+++ b/app/models/work_package/exports/csv.rb
@@ -29,6 +29,7 @@
module WorkPackage::Exports
class CSV < QueryExporter
include ::Exports::Concerns::CSV
+ include AdditionalColumns
alias :records :work_packages
diff --git a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
index 482b820fa2e..45f3e85e55c 100644
--- a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
+++ b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
@@ -1,6 +1,37 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
module XlsExport::WorkPackage::Exporter
class XLS < WorkPackage::Exports::QueryExporter
include ::XlsExport::Concerns::SpreadsheetBuilder
+ include WorkPackage::Exports::AdditionalColumns
def records
work_packages
diff --git a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
index f29bf999bfe..25723cbf0a3 100644
--- a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
+++ b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
@@ -325,31 +325,45 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
end
let(:work_packages) { [work_package] }
- let(:column_names) { %w[subject status updated_at estimated_hours] }
-
- it "does not output anything" do
- work_package.reload
- estimated_cell = sheet.rows.last.to_a.last
- expect(estimated_cell).to be_nil
+ let(:column_names) { %w[estimated_hours remaining_hours] }
+
+ it "outputs its raw value in an additional 'Total work' column right after the 'Work' column" do
+ headers_row, values_row = sheet.rows
+ # why is there a nil at the end of the headers row?
+ headers_row.compact!
+ expect(headers_row.zip(values_row)).to eq [
+ ["Work", nil],
+ ["Total work", "15.0"],
+ ["Remaining work", nil],
+ ["Total remaining work", nil]
+ ]
end
end
- describe "with estimated hours and derived estimated hours" do
+ describe "with estimated hours, derived estimated hours, remaining hours, and derived remaining hours" do
let(:work_package) do
create(:work_package,
project:,
estimated_hours: 10.0,
derived_estimated_hours: 15.0,
+ remaining_hours: 5.0,
+ derived_remaining_hours: 8.0,
type: project.types.first)
end
let(:work_packages) { [work_package] }
- let(:column_names) { %w[subject status updated_at estimated_hours] }
-
- it "outputs only the unformatted estimated hours" do
- work_package.reload
- estimated_cell = sheet.rows.last.to_a.last
- expect(estimated_cell).to eq "10.0"
+ let(:column_names) { %w[estimated_hours remaining_hours] }
+
+ it "outputs all raw values in 4 distinct columns" do
+ headers_row, values_row = sheet.rows
+ # why is there a nil at the end of the headers row?
+ headers_row.compact!
+ expect(headers_row.zip(values_row)).to eq [
+ ["Work", "10.0"],
+ ["Total work", "15.0"],
+ ["Remaining work", "5.0"],
+ ["Total remaining work", "8.0"]
+ ]
end
end
@@ -365,13 +379,18 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do
end
let(:work_packages) { [work_package] }
- let(:column_names) { %w[subject status updated_at estimated_hours remaining_hours] }
-
- it "outputs the unformatted values without the derived values" do
- work_package.reload
- estimated_cell, remaining_cell = sheet.rows.last.to_a.last(2)
- expect(estimated_cell).to eq "0.0"
- expect(remaining_cell).to eq "0.0"
+ let(:column_names) { %w[estimated_hours remaining_hours] }
+
+ it "outputs raw values in 4 distinct columns" do
+ headers_row, values_row = sheet.rows
+ # why is there a nil at the end of the headers row?
+ headers_row.compact!
+ expect(headers_row.zip(values_row)).to eq [
+ ["Work", "0.0"],
+ ["Total work", "15.0"],
+ ["Remaining work", "0.0"],
+ ["Total remaining work", "8.0"]
+ ]
end
end
end
diff --git a/spec/models/work_package/exporter/csv_integration_spec.rb b/spec/models/work_package/exporter/csv_integration_spec.rb
index db0f5576aac..b910fb1018a 100644
--- a/spec/models/work_package/exporter/csv_integration_spec.rb
+++ b/spec/models/work_package/exporter/csv_integration_spec.rb
@@ -50,6 +50,10 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" do
described_class.new(query, options)
end
+ def byte_order_mark
+ "\uFEFF"
+ end
+
context "when the query is default" do
let(:query) do
create(:query, project:, user:, column_names: %i(type subject assigned_to updated_at estimated_hours))
@@ -74,37 +78,38 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" do
)
end
- context "when description is included" do
- it "performs a successful export" do
- work_package.reload
-
- data = CSV.parse instance.export!.content
+ subject(:header_value_pairs) do
+ data = CSV.parse instance.export!.content
+ headers, values = data
+ headers.zip(values)
+ end
- expect(data.size).to eq(2)
- expect(data.last).to include(work_package.type.name)
- expect(data.last).to include(work_package.subject)
- expect(data.last).to include(work_package.description)
- expect(data.last).to include(user.name)
- expect(data.last).to include(work_package.updated_at.in_time_zone(user.time_zone).strftime("%m/%d/%Y %I:%M %p"))
- expect(data.last).to include("10.0")
+ context "when description is included" do
+ it "performs a successful export with description column" do
+ expect(header_value_pairs).to eq [
+ ["#{byte_order_mark}Type", work_package.type.name],
+ ["Subject", work_package.subject],
+ ["Assignee", user.name],
+ ["Updated on", work_package.updated_at.in_time_zone(user.time_zone).strftime("%m/%d/%Y %I:%M %p")],
+ ["Work", "10.0"],
+ ["Total work", "15.0"],
+ ["Description", work_package.description]
+ ]
end
end
context "when description is not included" do
let(:options) { { show_descriptions: false } }
- it "performs a successful export" do
- work_package.reload
-
- data = CSV.parse instance.export!.content
-
- expect(data.size).to eq(2)
- expect(data.last).to include(work_package.type.name)
- expect(data.last).to include(work_package.subject)
- expect(data.last).not_to include(work_package.description)
- expect(data.last).to include(user.name)
- expect(data.last).to include(work_package.updated_at.in_time_zone(user.time_zone).strftime("%m/%d/%Y %I:%M %p"))
- expect(data.last).to include("10.0")
+ it "performs a successful export without description column" do
+ expect(header_value_pairs).to eq [
+ ["#{byte_order_mark}Type", work_package.type.name],
+ ["Subject", work_package.subject],
+ ["Assignee", user.name],
+ ["Updated on", work_package.updated_at.in_time_zone(user.time_zone).strftime("%m/%d/%Y %I:%M %p")],
+ ["Work", "10.0"],
+ ["Total work", "15.0"]
+ ]
end
end
end
@@ -128,7 +133,7 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" do
data = CSV.parse instance.export!.content
expect(data.size).to eq(5)
- expect(data.pluck(0)).to eq ["\xEF\xBB\xBFType", "Type A", "Type A", "Type A", "Type B"]
+ expect(data.pluck(0)).to eq ["#{byte_order_mark}Type", "Type A", "Type A", "Type A", "Type B"]
end
end
@@ -173,7 +178,7 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" do
data = CSV.parse instance.export!.content
expect(data.size).to eq(5)
- expect(data.pluck(0)).to eq %w[WP4 WP2 WP1 WP3].unshift("\xEF\xBB\xBFSubject")
+ expect(data.pluck(0)).to eq %w[WP4 WP2 WP1 WP3].unshift("#{byte_order_mark}Subject")
end
end
end