2013-02-07 38 views
2

我正在開發一個使用我的以下代碼的Excel插件。 我創建了一個類庫,並添加以下代碼使用Excel添加插件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
    using System.Runtime.InteropServices; 
    using Microsoft.Win32; 

    namespace MyCustomAutomation 
    { 

// Replace the Guid below with your own guid that 

// you generate using Create GUID from the Tools menu 

[Guid("5268ABE2-9B09-439d-BE97-2EA60E103EF6")] 
[ClassInterface(ClassInterfaceType.AutoDual)] 
[ComVisible(true)] 
public class MyFunctions 
{ 
    public MyFunctions() 
    { 

    } 

    public double MultiplyNTimes(double number1, double number2, double timesToMultiply) 
    { 
     double result = number1; 
     for (double i = 0; i < timesToMultiply; i++) 
     { 
      result = result * number2; 
     } 

     return result; 


    } 


    [ComRegisterFunctionAttribute] 
    public static void RegisterFunction(Type type) 
    { 

     Registry.ClassesRoot.CreateSubKey(

      GetSubKeyName(type, "Programmable")); 

     RegistryKey key = Registry.ClassesRoot.OpenSubKey(

      GetSubKeyName(type, "InprocServer32"), true); 

     key.SetValue("", 

      System.Environment.SystemDirectory + @"\mscoree.dll", 

      RegistryValueKind.String); 
    } 

    [ComUnregisterFunctionAttribute] 
    public static void UnregisterFunction(Type type) 
    { 

     Registry.ClassesRoot.DeleteSubKey(

      GetSubKeyName(type, "Programmable"), false); 
    } 

    private static string GetSubKeyName(Type type, 

     string subKeyName) 
    { 

     System.Text.StringBuilder s = 

      new System.Text.StringBuilder(); 

     s.Append(@"CLSID\{"); 

     s.Append(type.GUID.ToString().ToUpper()); 

     s.Append(@"}\"); 

     s.Append(subKeyName); 

     return s.ToString(); 

    } 
    } 
    } 

我安裝了它,功能正常工作在Excel中。我是能夠使用MultiplyNTimes的函數,返回value.However我的問題是,當我打電話功能從單元格結果顯示在同一個單元格本身,而我希望結果在被稱爲單元格以外的任何單元格中播放。我完全無能爲力,因爲我無法達到任何方向。 請幫忙

+0

那麼你想要的結果,其細胞放在哪裏? –

+0

可以說,如果公式是用A1寫的我希望答案在B1或C1中。 – Rohit

+0

配置其他單元格的公式對於Excel用戶來說完全違反直覺。也許你應該重新考慮你的設計。 –

回答

1

要在另一個單元格中獲得結果,只需將結果作爲數組返回即可。從而爲您的MultiplyNTimes功能的另一個版本爲例,可以寫成:

public double[] MultiplyNTimesNextCell(double number1, double number2, double timesToMultiply) 
{ 
     // result[0] is the value returned on the first cell 
     // result[1] is the value returned on the next cell 
     double[] result = new double[] {0, number1}; 
     for (double i = 0; i < timesToMultiply; i++) 
     { 
      // hardcoded to result[1] where to return the result 
      result[1] = result[1] * number2; 
     } 

     return result; 
} 

然後當你在Excel中使用此功能,您必須使用數組公式語法,如果你輸入的功能,這意味着在A1中,然後選擇單元格A1和B1,然後按F2然後按Ctrl + Shift + Enter鍵入

另請注意,我只是在輸入公式的單元格中返回0。如果您希望將其更改爲另一個不同類型的值,則可以使用一個對象數組作爲結果數據類型。

因此,例如,你可以用這種方式重寫:

public object[] MultiplyNTimesObj(double number1, double number2, double timesToMultiply) 
{ 
    object[] result = new object[] { "MultiplyNTimesObj=", number1 }; 
    for (double i = 0; i < timesToMultiply; i++) 
    { 
     result[1] = (double)result[1] * number2; 
    } 

    return result; 
}