2013-02-07 2 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(); 

    } 
    } 
    } 

나는 그것을 설치하고 기능이 엑셀 .I에서 잘 작동 내 문제는 내가 전화를 할 때한다는 것이다 value.However을 반환 MultiplyNTimes 기능을 사용할 수 있어요 추가 함수를 셀에서 호출하면 동일한 셀 자체에 결과가 표시되는 반면 호출 된 셀 이외의 다른 셀에서는 결과가 표시되기를 원합니다. 어떤 방향으로도 연결할 수 없으므로 완전히 단서가 없습니다. 도와주세요

+0

그래서 당신은 결과를 원하는 않는 셀에 배치 할 수? –

+0

수식이 A1로 작성 되었으면 답이 B1 또는 C1이되기를 원합니다. – Rohit

+0

다른 셀을 수식으로 설정하면 Excel 사용자가 완전히 직관적입니다. 아마도 디자인을 재고해야합니다. –

답변

1

결과를 다른 셀에 넣으려면 결과를 배열로 반환하면됩니다.

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에서이 기능을 사용할 때 다음, 당신이 사용해야 당신이 기능을 입력하면 의미 배열 수식 구문 : 당신의 MultiplyNTimes 기능의 또 다른 버전 예로 그래서,이 같이 쓸 수있다 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; 
} 
관련 문제