マージ

列単位や行単位で同一内容をもつ隣接セルをひとつのセルとしてグループ表示させることができます。SetColumnMergeメソッドおよびSetRowMergeメソッドにて、セルのマージができます。

このサンプルでは、ボタンを押下することで列単位でマージの動作を確認することができます。

「マージの解除」:セルのマージを解除します。
「常にマージ」:同一列の隣接するセルが同じ値であれば、マージします。
「他の列にあわせてマージ」:同一列の隣接するセルが同じ値で隣の列もマージされていれば、マージします。
 プロジェクト名分類プライオリティ担当者期限対応日ステータス
1グレープ商事機能改善1佐藤2010/06/302009/09/10新規
2グレープ商事機能改善1佐藤2010/06/302009/09/10対応中
3グレープ商事機能改善1佐藤2010/06/302009/09/10対応中
4グレープ商事バグ2山田2010/06/302009/09/10新規
5グレープ商事バグ2山田2010/06/302009/09/10新規
6グレープ商事バグ3山田2010/06/302009/09/10新規
7グレープ商事バグ3山田2010/06/302009/09/10新規
8紫山工務店機能改善1高橋2009/08/152009/07/20対応済み
9紫山工務店バグ1高橋2009/08/152009/08/04対応済み
10紫山工務店バグ2高橋2009/08/152009/08/05対応済み
   

ソースコード

別ウィンドウで表示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class rowcolcell_rowcolmerge : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }

        // SPREADの設定
        InitSpread(FpSpread1);

        // シート設定
        InitSpreadStyles(FpSpread1.Sheets[0]);

        // クライアント側スクリプトの設定
        string clientScript = "<script language=\"JavaScript\">";
        clientScript += "function MergeNone()";
        clientScript += "{";
        clientScript += "var spread = document.getElementById(\"" + FpSpread1.ClientID + "\");";
        clientScript += "spread.CallBack(\"MergeNone\");";
        clientScript += "}";
        clientScript += "function MergeAlways()";
        clientScript += "{";
        clientScript += "var spread = document.getElementById(\"" + FpSpread1.ClientID + "\");";
        clientScript += "spread.CallBack(\"MergeAlways\");";
        clientScript += "}";
        clientScript += "function MergeRestricted()";
        clientScript += "{";
        clientScript += "var spread = document.getElementById(\"" + FpSpread1.ClientID + "\");";
        clientScript += "spread.CallBack(\"MergeRestricted\");";
        clientScript += "}";
        clientScript += "</script>";

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "rowcolmergeScript", clientScript, false);
    }

    private void InitSpread(FarPoint.Web.Spread.FpSpread spread)
    {
        spread.CssClass = "spreadStyle";
        spread.UseClipboard = false;
    }

    private void InitSpreadStyles(FarPoint.Web.Spread.SheetView sheet)
    {
        // フォントサイズの設定
        sheet.DefaultStyle.Font.Size = FontUnit.Parse("80%");
        sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%");
        sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%");
        sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Parse("80%");

        // 縦方向の揃え位置を中央に設定
        sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle;

        sheet.ColumnCount = 7;
        sheet.RowCount = 10;

        // 列幅の設定
        sheet.Columns[0].Width = 100;
        sheet.Columns[2].Width = 100;

        // セル型の設定
        FarPoint.Web.Spread.IntegerCellType ic = new FarPoint.Web.Spread.IntegerCellType();
        sheet.Columns[2].CellType = ic;
        FarPoint.Web.Spread.DateTimeCellType dc = new FarPoint.Web.Spread.DateTimeCellType();
        sheet.Columns[4, 5].CellType = dc;

        // 列名の設定
        sheet.ColumnHeader.Cells[0, 0].Value = "プロジェクト名";
        sheet.ColumnHeader.Cells[0, 1].Value = "分類";
        sheet.ColumnHeader.Cells[0, 2].Value = "プライオリティ";
        sheet.ColumnHeader.Cells[0, 3].Value = "担当者";
        sheet.ColumnHeader.Cells[0, 4].Value = "期限";
        sheet.ColumnHeader.Cells[0, 5].Value = "対応日";
        sheet.ColumnHeader.Cells[0, 6].Value = "ステータス";

        // テストデータの設定
        sheet.Cells[0, 0, 6, 0].Value = "グレープ商事";
        sheet.Cells[7, 0, 9, 0].Value = "紫山工務店";

        sheet.Cells[0, 1, 2, 1].Value = "機能改善";
        sheet.Cells[3, 1, 6, 1].Value = "バグ";
        sheet.Cells[7, 1].Value = "機能改善";
        sheet.Cells[8, 1, 9, 1].Value = "バグ";

        sheet.Cells[0, 2, 2, 2].Value = 1;
        sheet.Cells[3, 2, 4, 2].Value = 2;
        sheet.Cells[5, 2, 6, 2].Value = 3;
        sheet.Cells[7, 2, 8, 2].Value = 1;
        sheet.Cells[9, 2].Value = 2;

        sheet.Cells[0, 3, 2, 3].Value = "佐藤";
        sheet.Cells[3, 3, 6, 3].Value = "山田";
        sheet.Cells[7, 3, 9, 3].Value = "高橋";

        sheet.Cells[0, 4, 6, 4].Value = DateTime.Parse("2010/06/30");
        sheet.Cells[7, 4, 9, 4].Value = DateTime.Parse("2009/08/15");

        sheet.Cells[0, 5, 6, 5].Value = DateTime.Parse("2009/9/10");
        sheet.Cells[7, 5].Value = DateTime.Parse("2009/07/20");
        sheet.Cells[8, 5].Value = DateTime.Parse("2009/08/04");
        sheet.Cells[9, 5].Value = DateTime.Parse("2009/08/05");

        sheet.Cells[0, 6].Value = "新規";
        sheet.Cells[1, 6, 2, 6].Value = "対応中";
        sheet.Cells[3, 6, 6, 6].Value = "新規";
        sheet.Cells[7, 6, 9, 6].Value = "対応済み";
    }
    protected void FpSpread1_ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
        if (e.CommandName == "MergeNone")
        {
            // マージの設定
            FpSpread1.ActiveSheetView.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.None);
        }
        else if (e.CommandName == "MergeAlways")
        {
            // マージの設定
            FpSpread1.ActiveSheetView.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always);
        }
        else if (e.CommandName == "MergeRestricted")
        {
            // マージの設定
            FpSpread1.ActiveSheetView.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Restricted);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class rowcolcell_rowcolmerge
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            Return
        End If

        ' SPREADの設定
        InitSpread(FpSpread1)

        ' シート設定
        InitSpreadStyles(FpSpread1.Sheets(0))

        ' クライアント側スクリプトの設定
        Dim clientScript As String = "<script language=""JavaScript"">"
        clientScript += "function MergeNone()"
        clientScript += "{"
        clientScript += "var spread = document.getElementById(""" + FpSpread1.ClientID & """);"
        clientScript += "spread.CallBack(""MergeNone"");"
        clientScript += "}"
        clientScript += "function MergeAlways()"
        clientScript += "{"
        clientScript += "var spread = document.getElementById(""" + FpSpread1.ClientID & """);"
        clientScript += "spread.CallBack(""MergeAlways"");"
        clientScript += "}"
        clientScript += "function MergeRestricted()"
        clientScript += "{"
        clientScript += "var spread = document.getElementById(""" + FpSpread1.ClientID & """);"
        clientScript += "spread.CallBack(""MergeRestricted"");"
        clientScript += "}"
        clientScript += "</script>"

        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "rowcolmergeScript", clientScript, False)
    End Sub

    Private Sub InitSpread(ByVal spread As FarPoint.Web.Spread.FpSpread)
        spread.CssClass = "spreadStyle"
        spread.UseClipboard = False
    End Sub

    Private Sub InitSpreadStyles(ByVal sheet As FarPoint.Web.Spread.SheetView)
        ' フォントサイズの設定
        sheet.DefaultStyle.Font.Size = FontUnit.Parse("80%")
        sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%")
        sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%")
        sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Parse("80%")

        ' 縦方向の揃え位置を中央に設定
        sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle

        sheet.ColumnCount = 7
        sheet.RowCount = 10

        ' 列幅の設定
        sheet.Columns(0).Width = 100
        sheet.Columns(2).Width = 100

        ' セル型の設定
        Dim ic As New FarPoint.Web.Spread.IntegerCellType()
        sheet.Columns(2).CellType = ic
        Dim dc As New FarPoint.Web.Spread.DateTimeCellType()
        sheet.Columns(4, 5).CellType = dc

        ' 列名の設定
        sheet.ColumnHeader.Cells(0, 0).Value = "プロジェクト名"
        sheet.ColumnHeader.Cells(0, 1).Value = "分類"
        sheet.ColumnHeader.Cells(0, 2).Value = "プライオリティ"
        sheet.ColumnHeader.Cells(0, 3).Value = "担当者"
        sheet.ColumnHeader.Cells(0, 4).Value = "期限"
        sheet.ColumnHeader.Cells(0, 5).Value = "対応日"
        sheet.ColumnHeader.Cells(0, 6).Value = "ステータス"

        ' テストデータの設定
        sheet.Cells(0, 0, 6, 0).Value = "グレープ商事"
        sheet.Cells(7, 0, 9, 0).Value = "紫山工務店"

        sheet.Cells(0, 1, 2, 1).Value = "機能改善"
        sheet.Cells(3, 1, 6, 1).Value = "バグ"
        sheet.Cells(7, 1).Value = "機能改善"
        sheet.Cells(8, 1, 9, 1).Value = "バグ"

        sheet.Cells(0, 2, 2, 2).Value = 1
        sheet.Cells(3, 2, 4, 2).Value = 2
        sheet.Cells(5, 2, 6, 2).Value = 3
        sheet.Cells(7, 2, 8, 2).Value = 1
        sheet.Cells(9, 2).Value = 2

        sheet.Cells(0, 3, 2, 3).Value = "佐藤"
        sheet.Cells(3, 3, 6, 3).Value = "山田"
        sheet.Cells(7, 3, 9, 3).Value = "高橋"

        sheet.Cells(0, 4, 6, 4).Value = DateTime.Parse("2010/06/30")
        sheet.Cells(7, 4, 9, 4).Value = DateTime.Parse("2009/08/15")

        sheet.Cells(0, 5, 6, 5).Value = DateTime.Parse("2009/9/10")
        sheet.Cells(7, 5).Value = DateTime.Parse("2009/07/20")
        sheet.Cells(8, 5).Value = DateTime.Parse("2009/08/04")
        sheet.Cells(9, 5).Value = DateTime.Parse("2009/08/05")

        sheet.Cells(0, 6).Value = "新規"
        sheet.Cells(1, 6, 2, 6).Value = "対応中"
        sheet.Cells(3, 6, 6, 6).Value = "新規"
        sheet.Cells(7, 6, 9, 6).Value = "対応済み"
    End Sub

    Protected Sub FpSpread1_ButtonCommand(ByVal sender As Object, ByVal e As FarPoint.Web.Spread.SpreadCommandEventArgs) Handles FpSpread1.ButtonCommand
        If e.CommandName = "MergeNone" Then
            ' マージの設定
            FpSpread1.ActiveSheetView.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.None)
        ElseIf e.CommandName = "MergeAlways" Then
            ' マージの設定
            FpSpread1.ActiveSheetView.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always)
        ElseIf e.CommandName = "MergeRestricted" Then
            ' マージの設定
            FpSpread1.ActiveSheetView.SetColumnMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Restricted)
        End If
    End Sub
End Class

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" 
    CodeFile="rowcolmerge.aspx.cs" Inherits="rowcolcell_rowcolmerge" %>

<%@ Register Assembly="FarPoint.Web.SpreadJ" Namespace="FarPoint.Web.Spread" TagPrefix="FarPoint" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeaderPlaceHolder1" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <input id="Button1" type="button" value="マージの解除" onclick="MergeNone();" style="width: 160px;" />
    <input id="Button2" type="button" value="常にマージ" onclick="MergeAlways();" style="width: 160px;" />
    <input id="Button3" type="button" value="他の列にあわせてマージ" onclick="MergeRestricted();" style="width: 160px;" />
    <FarPoint:FpSpread ID="FpSpread1" runat="server" BorderColor="#A0A0A0" BorderStyle="Solid"
        BorderWidth="1px" onbuttoncommand="FpSpread1_ButtonCommand">
        <CommandBar BackColor="#F6F6F6" ButtonFaceColor="Control" ButtonHighlightColor="ControlLightLight"
            ButtonShadowColor="ControlDark" />
        <Sheets>
            <FarPoint:SheetView SheetName="Sheet1">
            </FarPoint:SheetView>
        </Sheets>
    </FarPoint:FpSpread>
</asp:Content>