using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;

namespace OfficeAppHelp.Word
{
    /// <summary>
    /// 表格扩展
    /// </summary>
    public static class TableEx
    {
        /// <summary>
        /// 得到表格的宽度
        /// </summary>
        /// <param name="table">表格，是标准长方形没有进行合并单元格的表格</param>
        /// <returns></returns>
        public static float GetWidth(this Table table)
        {
            float f = 0f;
            for (int i = 1; i <= table.Columns.Count; i++)
            {
                f += table.Columns[i].Width;
            }
            return f;
        }

        /// <summary>
        /// 得到表格的宽度
        /// </summary>
        /// <param name="table">表格，是标准长方形没有进行合并单元格的表格</param>
        /// <returns></returns>
        public static float GetHeight(this Table table)
        {
            float f = 0f;
            for (int i = 1; i <= table.Rows.Count; i++)
            {
                f += table.Rows[i].Height;
            }
            return f;
        }

        /// <summary>
        /// 依次将文本添加到单元格
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="texts">文本集合</param>
        public static void AddText(this Table table, params string[] texts)
        {
            if (table == null || texts == null)
                return;

            int i = 0;
            foreach (Row row in table.Rows)
            {
                foreach (Cell cell in row.Cells)
                {
                    cell.Range.Text = texts[i];
                    i++;

                    if (i >= texts.Length)
                        return;
                }
            }
        }

    }
}
