C# 中的 Excel 自动填充事件

发布于 2024-08-05 02:43:56 字数 71 浏览 6 评论 0原文

我正在使用 C# 编写一个 Excel 加载项,并且需要能够判断用户何时自动填充单元格。

如何捕获自动填充事件?

I'm writing an Excel add-in using C# and need to be able to tell when users autofill cells.

How can I catch the autofill event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2024-08-12 02:43:56

以下是我通过粘贴一系列单元格或拖动值来确定单个单元格是否已更改或多个单元格是否已更改的方法,

//Get the currentCell/s changed
        object missing = System.T 
        string currentCell = Target.get_Address(
        missing, missing, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1,  missing, missing);


//Split the currentCell variable this aids the logic in getting cell letter and number
         char[] delimiter = new char[] {'

我希望这在某种程度上有助于确定单元格的更改方式。

迪佐斯

}; string[] CellSplit = currentCell.Split(delimiter); //try parse the value in position 1 if this is true then a whole sheet has been pasted and needs to be checked //usually will be false if 1 cell is changed or multiple by dragging Int32 int32Val; bool result; result = Int32.TryParse(CellSplit.GetValue(1).ToString).Replace":", ""),System.Globalization.NumberStyles.Integer, null, out int32Val); //check the length of the array as this will help in determining how the cells have been changed int numCellSplit = CellSplit.Length; //the result is false if a single cell was changed and the length is 3 if (CellSplit.Length == 3 && result == false) { \\your code here } //the result is true if the whole sheet has been pasted if (CellSplit.Length == 3 && result == true) { \\your code here } //if the user dragged the data from one cell across others if (CellSplit.Length == 5 && result == false) { //create char arrays to get the Alphabetical letters to use to loop char[] FirstLetter = CellSplit.GetValue(1).ToString().ToCharArray(); char[] SecondLetter = CellSplit.GetValue(3).ToString().ToCharArray(); int Alphaletter = (char)FirstLetter.GetValue(0); int Betaletter = (char)SecondLetter.GetValue(0); //Alphabetical letters have an int value so they can be used to loop through the cells for (int CellCheck = Alphaletter; CellCheck <= Betaletter; CellCheck++) { int LoopStart = int.Parse(CellSplit.GetValue(2).ToString().Replace(":", "")); int LoopEnd = int.Parse(CellSplit.GetValue(4).ToString()); for (; LoopStart <= LoopEnd; LoopStart++) { //your code here } } }

我希望这在某种程度上有助于确定单元格的更改方式。

迪佐斯

Here is how I determmine if a single cell has changed or multiples either by pasting a range of cells or by draging values

//Get the currentCell/s changed
        object missing = System.T 
        string currentCell = Target.get_Address(
        missing, missing, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1,  missing, missing);


//Split the currentCell variable this aids the logic in getting cell letter and number
         char[] delimiter = new char[] {'

I hope this helps in some way to determine how the cells have been changed.

Deezos

}; string[] CellSplit = currentCell.Split(delimiter); //try parse the value in position 1 if this is true then a whole sheet has been pasted and needs to be checked //usually will be false if 1 cell is changed or multiple by dragging Int32 int32Val; bool result; result = Int32.TryParse(CellSplit.GetValue(1).ToString).Replace":", ""),System.Globalization.NumberStyles.Integer, null, out int32Val); //check the length of the array as this will help in determining how the cells have been changed int numCellSplit = CellSplit.Length; //the result is false if a single cell was changed and the length is 3 if (CellSplit.Length == 3 && result == false) { \\your code here } //the result is true if the whole sheet has been pasted if (CellSplit.Length == 3 && result == true) { \\your code here } //if the user dragged the data from one cell across others if (CellSplit.Length == 5 && result == false) { //create char arrays to get the Alphabetical letters to use to loop char[] FirstLetter = CellSplit.GetValue(1).ToString().ToCharArray(); char[] SecondLetter = CellSplit.GetValue(3).ToString().ToCharArray(); int Alphaletter = (char)FirstLetter.GetValue(0); int Betaletter = (char)SecondLetter.GetValue(0); //Alphabetical letters have an int value so they can be used to loop through the cells for (int CellCheck = Alphaletter; CellCheck <= Betaletter; CellCheck++) { int LoopStart = int.Parse(CellSplit.GetValue(2).ToString().Replace(":", "")); int LoopEnd = int.Parse(CellSplit.GetValue(4).ToString()); for (; LoopStart <= LoopEnd; LoopStart++) { //your code here } } }

I hope this helps in some way to determine how the cells have been changed.

Deezos

荒芜了季节 2024-08-12 02:43:56

经过多次尝试,我自己找到了解决方案。该解决方案并不理想,但似乎可行。

确保处理工作表更改事件:

displayWorksheet.Change += new Excel.DocEvents_ChangeEventHandler(CellsChange);

处理该事件的方法应如下所示:

private void CellsChange(Excel.Range Target) 
{
    if ((this.Application.Selection as Excel.Range) != null)
    {
        Excel.Range selectedRange = thisAddIn.Application.Selection as Excel.Range;
        if (selectedRange.Rows.Count > 1)
        {
            //put your code to handle autofill here
        }
    }
}

I found the solution myself after many attempts. The solution is not ideal but it seams to work.

Make sure to handle the worksheet Change event:

displayWorksheet.Change += new Excel.DocEvents_ChangeEventHandler(CellsChange);

the method to handle it should then look like this:

private void CellsChange(Excel.Range Target) 
{
    if ((this.Application.Selection as Excel.Range) != null)
    {
        Excel.Range selectedRange = thisAddIn.Application.Selection as Excel.Range;
        if (selectedRange.Rows.Count > 1)
        {
            //put your code to handle autofill here
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文