Skip to content

Commit

Permalink
add batch formatting methods to range class
Browse files Browse the repository at this point in the history
  • Loading branch information
tfussell committed Mar 19, 2017
1 parent d7ee03d commit 8f4c82d
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
47 changes: 47 additions & 0 deletions include/xlnt/worksheet/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,48 @@ class XLNT_API range
///
/// </summary>
bool contains(const cell_reference &ref);

/// <summary>
///
/// </summary>
range alignment(const xlnt::alignment &new_alignment);

/// <summary>
///
/// </summary>
range border(const xlnt::border &new_border);

/// <summary>
///
/// </summary>
range fill(const xlnt::fill &new_fill);

/// <summary>
///
/// </summary>
range font(const xlnt::font &new_font);

/// <summary>
///
/// </summary>
range number_format(const xlnt::number_format &new_number_format);

/// <summary>
///
/// </summary>
range protection(const xlnt::protection &new_protection);

/// <summary>
/// Sets the named style applied to all cells in this range to a style named style_name.
/// </summary>
range style(const class style &new_style);

/// <summary>
/// Sets the named style applied to all cells in this range to a style named style_name.
/// If this style has not been previously created in the workbook, a
/// key_not_found exception will be thrown.
/// </summary>
range style(const std::string &style_name);

/// <summary>
///
Expand Down Expand Up @@ -197,6 +239,11 @@ class XLNT_API range
/// </summary>
const_reverse_iterator crend() const;

/// <summary>
///
/// </summary>
void apply(std::function<void(class cell)> f);

private:
/// <summary>
///
Expand Down
Empty file.
61 changes: 61 additions & 0 deletions source/worksheet/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file

#include <xlnt/cell/cell.hpp>
#include <xlnt/styles/style.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <xlnt/worksheet/const_range_iterator.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/range_iterator.hpp>
Expand Down Expand Up @@ -96,6 +99,64 @@ bool range::contains(const cell_reference &ref)
&& ref_.bottom_right().row() >= ref.row();
}

range range::alignment(const xlnt::alignment &new_alignment)
{
apply([&new_alignment](class cell c) { c.alignment(new_alignment); });
return *this;
}

range range::border(const xlnt::border &new_border)
{
apply([&new_border](class cell c) { c.border(new_border); });
return *this;
}

range range::fill(const xlnt::fill &new_fill)
{
apply([&new_fill](class cell c) { c.fill(new_fill); });
return *this;
}

range range::font(const xlnt::font &new_font)
{
apply([&new_font](class cell c) { c.font(new_font); });
return *this;
}

range range::number_format(const xlnt::number_format &new_number_format)
{
apply([&new_number_format](class cell c) { c.number_format(new_number_format); });
return *this;
}

range range::protection(const xlnt::protection &new_protection)
{
apply([&new_protection](class cell c) { c.protection(new_protection); });
return *this;
}

range range::style(const class style &new_style)
{
apply([&new_style](class cell c) { c.style(new_style); });
return *this;
}

range range::style(const std::string &style_name)
{
return style(ws_.workbook().style(style_name));
}

void range::apply(std::function<void(class cell)> f)
{
for (auto row : *this)
{
for (auto cell : row)
{
f(cell);
}
}
}

cell range::cell(const cell_reference &ref)
{
return (*this)[ref.row() - 1][ref.column().index - 1];
Expand Down
40 changes: 40 additions & 0 deletions source/worksheet/tests/test_range.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <iostream>
#include <cxxtest/TestSuite.h>

#include <xlnt/worksheet/header_footer.hpp>
#include <xlnt/worksheet/worksheet.hpp>
#include <xlnt/workbook/workbook.hpp>

class test_range : public CxxTest::TestSuite
{
public:
void test_batch_formatting()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();

for (auto row = 1; row <= 10; ++row)
{
for (auto column = 1; column <= 10; ++column)
{
ws.cell(column, row).value(xlnt::cell_reference(column, row).to_string());
}
}

ws.range("A1:A10").font(xlnt::font().name("Arial"));
ws.range("A1:J1").font(xlnt::font().bold(true));

TS_ASSERT_EQUALS(ws.cell("A1").font().name(), "Calibri");
TS_ASSERT(ws.cell("A1").font().bold());

TS_ASSERT_EQUALS(ws.cell("A2").font().name(), "Arial");
TS_ASSERT(!ws.cell("A2").font().bold());

TS_ASSERT_EQUALS(ws.cell("B1").font().name(), "Calibri");
TS_ASSERT(ws.cell("B1").font().bold());

TS_ASSERT(!ws.cell("B2").has_format());
}
};

0 comments on commit 8f4c82d

Please sign in to comment.