2013-01-11 3 views
1

범위 (이 시점에서 1d)를 내 함수에 전달하고 범위의 수식을 포함하는 문자열 배열을 반환하고자합니다.ExcelDNA를 사용하는 범위에서 Excel 수식을 어떻게 구합니까?

여기 내 (작동하지 않는) 코드는 지금까지의 :

public static object[,] ReadFormulas([ExcelArgument(AllowReference=true)]object arg) 
    { 
     ExcelReference theRef = (ExcelReference)arg; 
     object[,] o = (object[,])theRef.GetValue(); 
     string[,] res = new string[o.GetLength(1),1]; 
     for(int i=0;i<o.GetLength(1);i++) 
     { 
      ExcelReference cellRef = new ExcelReference(theRef.RowFirst+i, theRef.ColumnFirst); 
      res[i,0] = XlCall.Excel(XlCall.xlfGetFormula, cellRef) as string; //Errors here 
     } 
     return res; 
    } 

답변

4

GET.FORMULA (xlfGetFormula) 기능은 매크로 시트에 허용됩니다. 워크 시트에서 호출하려면 엑셀-DNA의 기능은 다음과 같이 IsMacroType=true으로 표시해야합니다

[ExcelFunction(IsMacroType=true)] 
public static object[,] ReadFormulas(
     [ExcelArgument(AllowReference=true)]object arg) {...} 

는 또한, 당신이 당신의 루프에서 새로운 ExcelReference를 구성 할 때 조금주의해야합니다. 기본적으로 참조에 참조 된 시트는 현재 시트이며 전달 된 참조 시트는 아닙니다. SheetId를 새 ExcelReference에 명시 적으로 전달해야합니다. 색인 생성에 재미있는 내용이 있습니다. o.GetLength(1)은 의도 한 것이 아닙니다.

[ExcelFunction(IsMacroType=true)] 
public static object[,] ReadFormulasMacroType(
     [ExcelArgument(AllowReference=true)]object arg) 
{ 
    ExcelReference theRef = (ExcelReference)arg; 
    int rows = theRef.RowLast - theRef.RowFirst + 1; 
    object[,] res = new object[rows, 1]; 
    for(int i=0; i < rows; i++) 
    { 
     ExcelReference cellRef = new ExcelReference( 
      theRef.RowFirst+i, theRef.RowFirst+i, 
      theRef.ColumnFirst,theRef.ColumnFirst, 
      theRef.SheetId); 
     res[i,0] = XlCall.Excel(XlCall.xlfGetFormula, cellRef); 
    } 
    return res; 
} 
:

다음 버전이 작동 듯

관련 문제