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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[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.rbnew file mode 100644index 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+enddiff --git a/app/models/work_package/exports/csv.rb b/app/models/work_package/exports/csv.rbindex 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::Exportsclass CSV < QueryExporterinclude ::Exports::Concerns::CSV+ include AdditionalColumnsalias :records :work_packagesdiff --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.rbindex 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::Exporterclass XLS < WorkPackage::Exports::QueryExporterinclude ::XlsExport::Concerns::SpreadsheetBuilder+ include WorkPackage::Exports::AdditionalColumnsdef recordswork_packagesdiff --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.rbindex 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 doendlet(: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]+ ]endend- describe "with estimated hours and derived estimated hours" do+ describe "with estimated hours, derived estimated hours, remaining hours, and derived remaining hours" dolet(:work_package) docreate(: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)endlet(: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"]+ ]endend@@ -365,13 +379,18 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS doendlet(: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"]+ ]endendenddiff --git a/spec/models/work_package/exporter/csv_integration_spec.rb b/spec/models/work_package/exporter/csv_integration_spec.rbindex 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" dodescribed_class.new(query, options)end+ def byte_order_mark+ "\uFEFF"+ end+context "when the query is default" dolet(:query) docreate(: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]+ ]endendcontext "when description is not included" dolet(: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"]+ ]endendend@@ -128,7 +133,7 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" dodata = CSV.parse instance.export!.contentexpect(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"]endend@@ -173,7 +178,7 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" dodata = CSV.parse instance.export!.contentexpect(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")endendend