Beispiel #1
0
        private void SetAddPrintArea(int StartRowIndex, int StartColumnIndex, int EndRowIndex, int EndColumnIndex,
                                     bool ToAdd)
        {
            if (StartRowIndex < 1)
            {
                StartRowIndex = 1;
            }
            if (StartRowIndex > SLConstants.RowLimit)
            {
                StartRowIndex = SLConstants.RowLimit;
            }
            if (StartColumnIndex < 1)
            {
                StartColumnIndex = 1;
            }
            if (StartColumnIndex > SLConstants.ColumnLimit)
            {
                StartColumnIndex = SLConstants.ColumnLimit;
            }
            if (EndRowIndex < 1)
            {
                EndRowIndex = 1;
            }
            if (EndRowIndex > SLConstants.RowLimit)
            {
                EndRowIndex = SLConstants.RowLimit;
            }
            if (EndColumnIndex < 1)
            {
                EndColumnIndex = 1;
            }
            if (EndColumnIndex > SLConstants.ColumnLimit)
            {
                EndColumnIndex = SLConstants.ColumnLimit;
            }

            // no overlapping checked.

            var iSheetPosition = 0;
            int i;

            for (i = 0; i < slwb.Sheets.Count; ++i)
            {
                if (slwb.Sheets[i].Name.Equals(gsSelectedWorksheetName, StringComparison.OrdinalIgnoreCase))
                {
                    iSheetPosition = i;
                    break;
                }
            }

            var sPrintArea = string.Empty;

            if ((StartRowIndex == EndRowIndex) && (StartColumnIndex == EndColumnIndex))
            {
                sPrintArea = SLTool.ToCellReference(gsSelectedWorksheetName, StartRowIndex, StartColumnIndex, true);
            }
            else
            {
                sPrintArea = SLTool.ToCellRange(gsSelectedWorksheetName, StartRowIndex, StartColumnIndex, EndRowIndex,
                                                EndColumnIndex, true);
            }

            var bFound = false;

            for (i = 0; i < slwb.DefinedNames.Count; ++i)
            {
                if (slwb.DefinedNames[i].Name.Equals(SLConstants.PrintAreaDefinedName,
                                                     StringComparison.OrdinalIgnoreCase) &&
                    (slwb.DefinedNames[i].LocalSheetId != null) &&
                    (slwb.DefinedNames[i].LocalSheetId.Value == iSheetPosition))
                {
                    bFound = true;
                    if (ToAdd)
                    {
                        slwb.DefinedNames[i].Text = string.Format("{0},{1}", slwb.DefinedNames[i].Text, sPrintArea);
                    }
                    else
                    {
                        slwb.DefinedNames[i].Text = sPrintArea;
                    }
                }
            }

            if (!bFound)
            {
                var dn = new SLDefinedName(SLConstants.PrintAreaDefinedName);
                dn.LocalSheetId = (uint)iSheetPosition;
                dn.Text         = sPrintArea;
                slwb.DefinedNames.Add(dn);
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Set a given defined name. If it doesn't exist, a new defined name is created. If it exists, then the existing
        ///     defined name is overwritten.
        /// </summary>
        /// <param name="Name">
        ///     SheetName of defined name. Note that it cannot be a valid cell reference such as A1. It also cannot start
        ///     with "_xlnm" because it's reserved.
        /// </param>
        /// <param name="Text">The reference/content text of the defined name. For example, Sheet1!$A$1:$C$3</param>
        /// <param name="Comment">Comment for the defined name.</param>
        /// <param name="Scope">The name of the worksheet that the defined name is effective in.</param>
        /// <returns>True if the given defined name is created or an existing defined name is overwritten. False otherwise.</returns>
        public bool SetDefinedName(string Name, string Text, string Comment, string Scope)
        {
            Name = Name.Trim();
            if (SLTool.IsCellReference(Name))
            {
                return(false);
            }

            // these are reserved names
            if (Name.StartsWith("_xlnm"))
            {
                return(false);
            }

            if (Text.StartsWith("="))
            {
                if (Text.Length > 1)
                {
                    Text = Text.Substring(1);
                }
                else
                {
                    Text = "\"=\"";
                }
            }

            uint?iLocalSheetId = null;

            for (var i = 0; i < slwb.Sheets.Count; ++i)
            {
                if (slwb.Sheets[i].Name.Equals(Scope, StringComparison.OrdinalIgnoreCase))
                {
                    iLocalSheetId = (uint)i;
                    break;
                }
            }

            var bFound = false;
            var dn     = new SLDefinedName(Name);

            dn.Text = Text;
            if ((Comment != null) && (Comment.Length > 0))
            {
                dn.Comment = Comment;
            }
            if (iLocalSheetId != null)
            {
                dn.LocalSheetId = iLocalSheetId.Value;
            }
            foreach (var d in slwb.DefinedNames)
            {
                if (d.Name.Equals(Name, StringComparison.OrdinalIgnoreCase))
                {
                    bFound = true;
                    d.Text = Text;
                    if ((Comment != null) && (Comment.Length > 0))
                    {
                        d.Comment = Comment;
                    }
                    break;
                }
            }

            if (!bFound)
            {
                slwb.DefinedNames.Add(dn);
            }

            return(true);
        }