
var oServiceManagerObj = 0;
var oDesktopObj = 0;
var oPropertyValueObj = 0;
var oViewObj = 0;
var oSheetsObj = 0;

tCreateCalcOblects(fullFileName,Caption)
{
  oServiceManagerObj = CreateObject("com.sun.star.ServiceManager");
  oDesktopObj = oServiceManagerObj.createInstance("com.sun.star.frame.Desktop");
  oPropertyValueObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.beans.PropertyValue");

  var ar[1]; // некоторые параметры, которые можно задать при создании
  oPropertyValueObj.Name = "Hidden"; // Это команда скрытия окна Calc'а при ЗАГРУЗКЕ
  oPropertyValueObj.Value = true;
  ar[0] = oPropertyValueObj;

  string FilePath = convertFileNameToURL(fullFileName);
  _XlsObj = oDesktopObj.LoadComponentFromURL(FilePath, "_blank", 0, ar);
  oViewObj = _XlsObj.getCurrentController();
  oViewObj.getFrame().getContainerWindow().SetVisible(false);
  oSheetsObj = _XlsObj.getSheets();

  return 0;
}

tMakeCalcVisible()
{
  oViewObj.getFrame().getContainerWindow().SetVisible(true); // делаем Calc видимым

  return 0;
}

tGetCalcWshByIndex(i)
{
  _WshObj = oSheetsObj.getByIndex(i);
  _WshIndex = i;
  return 0;
}

tWriteStringToCalcCell(cell,str)
{
  FreeSpaceString(str,3,"");
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).setString(static_cast_to_string(str));

  return 0;
}

tWriteNumToCalcCell2(i,j,str)
{
  //var Cell = WSheet.getCellByPosition(Col-1, Row-1);
  // Cell.setString(t);
  FreeSpaceString(str,3,"");
  _WshObj.getCellByPosition(j-1,i-1).setString(static_cast_to_string(str));
  return 0;
}

tWriteNumToCalcCell(cell,num)
{
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).setValue(num);
  return 0;
}

tHorizontalAlignmentCalcCell(cell,n)
{ // right = 1; left = 2; center = 3
  int nj = 0;

  if ( n == 1 ) nj = 3; // rught
  if ( n == 2 ) nj = 1; // left
  if ( n == 3 ) nj = 2; // center

  _WshObj.getCellRangeByName(static_cast_to_string(cell)).HoriJustify = nj;

  return 0;
}

tFontSizeCalcCell(cell,n)
{
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).CharHeight = n;

  return 0;
}

tWshCalcSelect()
{
  return 0;
}

tCopyCalcRow(n)
{
  var oRangeAddrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellRangeAddress");
  oRangeAddrObj.Sheet = _WshIndex;
  oRangeAddrObj.StartColumn = 0;
  oRangeAddrObj.StartRow = n-1;
  oRangeAddrObj.EndColumn = 255;
  oRangeAddrObj.EndRow = n-1;  

  var oCellAdrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellAddress");
  oCellAdrObj.Sheet = _WshIndex;
  oCellAdrObj.Column = 0;
  oCellAdrObj.Row = n;
  _WshObj.CopyRange(oCellAdrObj, oRangeAddrObj);

  return 0;
}

tCopyCalcRows(n,count);
{
  var oRangeAddrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellRangeAddress");
  oRangeAddrObj.Sheet = _WshIndex;
  oRangeAddrObj.StartColumn = 0;
  oRangeAddrObj.StartRow = n-count;
  oRangeAddrObj.EndColumn = 255;
  oRangeAddrObj.EndRow = n-1;  

  var oCellAdrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellAddress");
  oCellAdrObj.Sheet = _WshIndex;
  oCellAdrObj.Column = 0;
  oCellAdrObj.Row = n;
  _WshObj.CopyRange(oCellAdrObj, oRangeAddrObj);

  return 0;
}

tCopyCalcRows2(n1,n2,count);
{
  var oRangeAddrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellRangeAddress");
  oRangeAddrObj.Sheet = _WshIndex;
  oRangeAddrObj.StartColumn = 0;
  oRangeAddrObj.StartRow = n1;
  oRangeAddrObj.EndColumn = 255;
  oRangeAddrObj.EndRow = n1+count-1;  

  var oCellAdrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellAddress");
  oCellAdrObj.Sheet = _WshIndex;
  oCellAdrObj.Column = 0;
  oCellAdrObj.Row = n2;
  _WshObj.CopyRange(oCellAdrObj, oRangeAddrObj);

  return 0;
}

tCopyCalcColumns(n,count)
{
  var oRangeAddrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellRangeAddress");
  oRangeAddrObj.Sheet = _WshIndex;
  oRangeAddrObj.StartColumn = n-count;
  oRangeAddrObj.StartRow = 0;  
  oRangeAddrObj.EndColumn = n-1;  
  oRangeAddrObj.EndRow = 512;      

  var oCellAdrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellAddress");
  oCellAdrObj.Sheet = _WshIndex;
  oCellAdrObj.Column = n;
  oCellAdrObj.Row = 0;
  _WshObj.CopyRange(oCellAdrObj, oRangeAddrObj);

  return 0;
}

tInsertCalcRows(n,count)
{
   _WshObj.getRows().insertByIndex(n,count);
  return 0;
}

tInsertCalcColumns(n,count)
{
   _WshObj.getColumns().insertByIndex(n,count);
  return 0;
}

tDeleteCalcRows(n,count)
{
  _WshObj.getRows().removeByIndex(n,count);
  return 0;
}

tDeleteCalcColumns(n,count)
{
  _WshObj.getColumns().removeByIndex(n,count);
  return 0;
}

tWshCalcCellSelect(n,m)
{
  return 0;
}

tWshCalcHide()
{
  _WshObj.IsVisible = false;
  return 0;
}

///////////////////////////////////////////

tCopyCalcWshByIndex(i,str) 
{
  oSheetsObj.copyByName("Sh",OemToAnsi(str),i); 
  _WshIndex = i;
  return 0;
}

tBordersCalcCell(cell,l,r,t,b,n) 
{
  var oPropertyBorderLine = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.BorderLine");
  oPropertyBorderLine.Color = 5;
  oPropertyBorderLine.InnerLineWidth = 0;
  oPropertyBorderLine.OuterLineWidth = n;
  oPropertyBorderLine.LineDistance = 0;
  // левая
  if (l) _WshObj.getCellRangeByName(static_cast_to_string(cell)).LeftBorder = oPropertyBorderLine; 
  // правая
  if (r) _WshObj.getCellRangeByName(static_cast_to_string(cell)).RightBorder = oPropertyBorderLine; 
  // верхняя
  if (t) _WshObj.getCellRangeByName(static_cast_to_string(cell)).TopBorder = oPropertyBorderLine; 
  // нижняя
  if (b) _WshObj.getCellRangeByName(static_cast_to_string(cell)).BottomBorder = oPropertyBorderLine; 

  return 0;
}

tMergeCellsCalcCell(cell,b) 
{
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).merge(false);
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).merge(b);
  return 0;
}

tMergeCellsCalc2(row1,col1,row2,col2,b)
{
  _WshObj.getCellRangeByPosition(col1,row1,col2,row2).merge(false);
  _WshObj.getCellRangeByPosition(col1,row1,col2,row2).merge(b);
  return 0;
}


tCalcPageBreaks(row)
{
  _WshObj.getRows().getByIndex(row).IsStartOfNewPage = true;
  return 0;
}

tCalcRowHeight(row,h)
{
  _WshObj.getRows().getByIndex(row).Height = 35*h;
  return 0;
}

tCalcColumnWidth(col,h)
{
  try
  {
   _WshObj.getColumns().getByName(static_cast_to_string(col)).Width = 210*h;
  }
  catch(...)
  {
   _WshObj.getColumns().getByIndex(col-1).Width = 210*h;
  }

  return 0;
}

tHorizontalAlignmentCalc(cell,i) 
{
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).HoriJustify = i;
  return 0;
}

tSaveCalcOblects(FileName)
{
   var oPropertyValueObj = 0;
   var args[1];
   oPropertyValueObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
   string sUrl = convertFileNameToURL(FileName);
   oPropertyValueObj.Name = "Overwrite";
   oPropertyValueObj.Value = false;     
   args[0] = oPropertyValueObj;
   _XlsObj.storeAsURL(sUrl, args);
   return 0;
}

tUnderlineCalc(cell,i)
{
  _WshObj.getCellRangeByName(static_cast_to_string(cell)).CharUnderline = i;
  return 0;
}

tAddCalcWorkbook(fullFileName,tmpnm)
{
  string FilePath = convertFileNameToURL(tmpnm);

  var ar[1];
  oPropertyValueObj.Name = "FilterName";
  oPropertyValueObj.Value = "MS Excel 97";
  ar[0] = oPropertyValueObj;
  _XlsObj.storeAsURL(FilePath,ar);
  oViewObj.getFrame().getContainerWindow().SetVisible(true); // делаем Calc видимым

  FilePath = convertFileNameToURL(fullFileName);
  oPropertyValueObj.Name = "Hidden";
  oPropertyValueObj.Value = true;
  ar[0] = oPropertyValueObj;
  _XlsObj = oDesktopObj.LoadComponentFromURL(FilePath, "_blank", 0, ar);
  oViewObj = _XlsObj.getCurrentController();
//oViewObj.getFrame().getContainerWindow().SetVisible(true); // делаем Calc видимым
  oSheetsObj = _XlsObj.getSheets();

  return 0;
}

tCopyCalcWshByName(i,name,templ)
{
  oSheetsObj.copyByName(templ,name,i-1);
  _WshObj = oSheetsObj.getByName(name);
  _WshIndex = i;

  return 0;
}

tSaveCalcWorkbook(tmpnm)
{
  string FilePath = convertFileNameToURL(tmpnm);

  var ar[1];
  oPropertyValueObj.Name = "FilterName";
  oPropertyValueObj.Value = "MS Excel 97";
  ar[0] = oPropertyValueObj;
  _XlsObj.storeAsURL(FilePath,ar);

  return 0;
}

tGetWshCountCalc(&tCount)
{
  tCount = oSheetsObj.getCount();
  return 0;
}

tWshCalcVisible(b)
{
  _WshObj.IsVisible = b;
  return 0;
}

tCopyCalcRowsWsh(wsh1,wsh2,n1,n2,count)
{
  var oRangeAddrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellRangeAddress");
  oRangeAddrObj.Sheet = wsh1;
  oRangeAddrObj.StartColumn = 0;
  oRangeAddrObj.StartRow = n1;
  oRangeAddrObj.EndColumn = 255;
  oRangeAddrObj.EndRow = n1+count-1;  

  var oCellAdrObj = oServiceManagerObj.Bridge_GetStruct("com.sun.star.table.CellAddress");
  oCellAdrObj.Sheet = wsh2;
  oCellAdrObj.Column = 0;
  oCellAdrObj.Row = n2;
  _WshObj.CopyRange(oCellAdrObj, oRangeAddrObj);
}

tCalcClearContents(row1,col1,row2,col2,LineS)
{
  // Даты, время, числа, строки
  _WshObj.getCellRangeByPosition(col1,row1,col2,row2).clearContents(1);
  _WshObj.getCellRangeByPosition(col1,row1,col2,row2).clearContents(2);
  _WshObj.getCellRangeByPosition(col1,row1,col2,row2).clearContents(4);
  if ( LineS )
   _WshObj.getCellRangeByPosition(col1,row1,col2,row2).clearContents(64);
  return 0;
}

tCalcQuit()
{
  oDesktopObj.terminate();
  oDesktopObj = 0;
  oServiceManagerObj = 0;

  return 0;
}

tCalcWshCount()
{
  return oSheetsObj.getCount();
}

tCalcRefWshByIndex(i)
{
  return oSheetsObj.getByIndex(i);
}

tDelCalcCol(sheet,n,count)
{
  sheet.getColumns().removeByIndex(n,count);
  return 0;
}

//-------------------------------------------------------------------------------------------------------

// получаем URL из пути к файлу (для открытия ОО)
convertFileNameToURL(FileName)
{
    char URL[256];  URL = FileName;
    
    for (int i = 1; i <= strlen(FileName); i++)
    {
        if ( URL[i]=='\\' ) URL[i]='/';
    }
    return "file:///" + URL;
}
//---------------------------------------------------------------------------
