
short PREFER_OFFICE = 0; // какой из офисных пакетов предпочтительлней: (значение нужно будет читать из настроек)
                         // 0 - Microsoft Office; 1 - OpenOffice

bool EXIST_EXCEL = false; // 1 - Ecxcel установлен; 0 - не установлен
bool EXIST_CALC  = false; // 1 - Calc установлен; 0 - не установлен

short MS_EXCEL = 0; // константа идентифицирующая Excel
short OO_CALC  = 1; // константа идентифицирующая Cacl

short TYPE_OFFICE = 0; // что в итоге выбрано для работы:
                       // Excel - TYPE_OFFICE == MS_EXCEL
                       // Calc  - TYPE_OFFICE == OO_CALC 

tPrepereOfficeForWork()
{ 
  // подготовительные действия
  PREFER_OFFICE = GetCommonCnfInt("PREF_OFFIC","печать");
  try
  { // проверяем установлен ли Excel
    var obj = CreateObject("Excel.Application");
    obj.Quit();
    EXIST_EXCEL = true; // Excel установлен
  }
  catch(...)
  {
    EXIST_EXCEL = false; // Excel не установлен
  }

  // если предпочтителен OpenOffice или нет Microsoft Office 
  // проверяем наличие OpenOffice
  if ( PREFER_OFFICE!=0 || !EXIST_EXCEL )
  {
     try
     { // проверяем установлен ли OpenOffice
       var sm = CreateObject("com.sun.star.ServiceManager");
       var desktop = sm.createInstance("com.sun.star.frame.Desktop");
   //  var prop = sm.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
   //  prop.Name = "Hidden";
   //  prop.Value = true;
   //  var ar[1];
   //  ar[0] = prop;
   //  var calcdocument = desktop.LoadComponentFromURL("private:factory/scalc", "_blank", 0, ar);
   //  calcdocument.close(false);
       desktop.terminate();
       desktop = 0;
       sm = 0;
       EXIST_CALC = true; // Calc вероятно установлен
     }
     catch(...)
     {
       EXIST_CALC = false; // Calc не установлен
     }
  }

  if ( PREFER_OFFICE == MS_EXCEL && EXIST_EXCEL ) TYPE_OFFICE = MS_EXCEL;
  else
  if ( PREFER_OFFICE == OO_CALC  && EXIST_CALC  ) TYPE_OFFICE = OO_CALC;
  else 
  if ( PREFER_OFFICE == MS_EXCEL && !EXIST_EXCEL && EXIST_CALC  ) TYPE_OFFICE = OO_CALC;
  else
  if ( PREFER_OFFICE == OO_CALC  && !EXIST_CALC  && EXIST_EXCEL ) TYPE_OFFICE = MS_EXCEL;
  else
  TYPE_OFFICE = -1;

  return TYPE_OFFICE;
}

var _XlsObj = 0; // таблица (xls)

tCreateXlsObjects(fullFileName,Caption)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCreateExcelObjects(fullFileName,Caption);
  if ( TYPE_OFFICE == OO_CALC ) tCreateCalcOblects(fullFileName,Caption);
  return 0;
}


tMakeXlsVisible()
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tMakeExcelVisible();
  if ( TYPE_OFFICE == OO_CALC ) tMakeCalcVisible();
  return 0;
}

var _WshObj = 0; // лист (worksheet) (wsh)
int _WshIndex = 0; // номер листа

tGetWshByIndex(i)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tGetExcelWshByIndex(i);
  if ( TYPE_OFFICE == OO_CALC ) tGetCalcWshByIndex(i-1);
  return 0;
}

tWriteStringToCell(cell,str)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWriteStringToExcelCell(cell,str);
  if ( TYPE_OFFICE == OO_CALC ) tWriteStringToCalcCell(cell,str);
  return 0;
}

tWriteNumToCell(cell,num)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWriteNumToExcelCell(cell,num);
  if ( TYPE_OFFICE == OO_CALC ) tWriteNumToCalcCell(cell,num);
  return 0;
}

tHorizontalAlignmentCell(cell,n)
{ // right = 1; left = 2; center = 3
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tHorizontalAlignmentExcelCell(cell,n);
  if ( TYPE_OFFICE == OO_CALC ) tHorizontalAlignmentCalcCell(cell,n);
  return 0;
}

tFontSizeCell(cell,n)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tFontSizeExcelCell(cell,n);
  if ( TYPE_OFFICE == OO_CALC ) tFontSizeCalcCell(cell,n);
  return 0;
}

tWshSelect()
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWshExcelSelect();
  if ( TYPE_OFFICE == OO_CALC ) tWshCalcSelect();
  return 0;
}

tCopyRow(n)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelRow(n);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcRow(n-1);
  return 0;
}

tCopyRows(n,count)
//копирует count строк, предшествующие строки n
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelRows(n,count);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcRows(n-1,count);
  return 0;
}

tCopyRows2(n1,n2,count)
//копирует count строк, начиная со строки n1, 
//и вставляет их со строки n2
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelRows2(n1,n2,count);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcRows2(n1-1,n2-1,count);
  return 0;
}

tCopyColumns(n,count)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelColumns(n,count);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcColumns(n-1,count);
  return 0;
}

tInsertRows(n,count)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tInsertExcelRows(n,count);
  if ( TYPE_OFFICE == OO_CALC ) tInsertCalcRows(n-1,count);
  return 0;
}

tInsertColumns(n,count)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tInsertExcelColumns(n,count);
  if ( TYPE_OFFICE == OO_CALC ) tInsertCalcColumns(n-1,count);
  return 0;
}

tDeleteRows(n,count)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tDeleteExcelRows(n,count);
  if ( TYPE_OFFICE == OO_CALC ) tDeleteCalcRows(n-1,count);
  return 0;
}

tDeleteColumns(n,count)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tDeleteExcelColumns(n,count);
  if ( TYPE_OFFICE == OO_CALC ) tDeleteCalcColumns(n-1,count);
  return 0;
}

tWshCellSelect(n,m)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWshExcelCellSelect(n,m);
  if ( TYPE_OFFICE == OO_CALC ) tWshCalcCellSelect(n,m);
  return 0;
}

tWshHide()
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWshExcelHide();
  if ( TYPE_OFFICE == OO_CALC ) tWshCalcHide();
  return 0;
}

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

tCopyWshByIndex(i,str) 
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelWshByIndex(i,str);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcWshByIndex(i-1,str);
  return 0;
}

tBordersCell(cell,l,r,t,b,n) 
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tBordersExcelCell(cell,l,r,t,b,n);
  if ( TYPE_OFFICE == OO_CALC ) tBordersCalcCell(cell,l,r,t,b,n);
  return 0;
}

tMergeCellsCell(cell,b)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tMergeCellsExcelCell(cell,b); 
  if ( TYPE_OFFICE == OO_CALC ) tMergeCellsCalcCell(cell,b);
  return 0;
}

tMergeCells2(row1,col1,row2,col2,b)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tMergeCellsExcel2(row1,col1,row2,col2,b);
  if ( TYPE_OFFICE == OO_CALC ) tMergeCellsCalc2(row1-1,col1-1,row2-1,col2-1,b);
  return 0;
}

tPageBreaks(row)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tExcelPageBreaks(row); 
  if ( TYPE_OFFICE == OO_CALC ) tCalcPageBreaks(row-1);
  return 0;
}

tRowHeight(row,h)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tExcelRowHeight(row,h); 
  if ( TYPE_OFFICE == OO_CALC ) tCalcRowHeight(row-1,h);
  return 0;
}

tColumnWidth(col,h)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tExcelColumnWidth(col,h); 
  if ( TYPE_OFFICE == OO_CALC ) tCalcColumnWidth(col,h);//???
  return 0;
}

tHorizontalAlignment(cell,i) 
{ // Горизонтальное выравнивание 
  // 1 -по левому краю
  // 2 -по центру
  // 3 -по правому краю
 
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tHorizontalAlignmentExcel(cell,i+1); 
  if ( TYPE_OFFICE == OO_CALC ) tHorizontalAlignmentCalc(cell,i);
  return 0;
}

tSaveXlsObjects(FileName)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tSaveExcelObjects(FileName);
  if ( TYPE_OFFICE == OO_CALC ) tSaveCalcOblects(FileName);
  return 0;
}

tUnderline(cell,i) 
{ // Подчеркивание
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tUnderlineExcel(cell,i); 
  if ( TYPE_OFFICE == OO_CALC ) tUnderlineCalc(cell,i);
  return 0;
}

tAddWorkbook(n,fullFileName,tmpnm)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tAddExcelWorkbook(n,fullFileName,tmpnm);
  if ( TYPE_OFFICE == OO_CALC ) tAddCalcWorkbook(fullFileName,tmpnm);
  return 0;
}

tCopyWshByName(i,name,templ)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelWshByName(i,name);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcWshByName(i,name,templ);
  return 0;
}

tSaveWorkbook(n,tmpnm)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tSaveExcelWorkbook(n,tmpnm);
  if ( TYPE_OFFICE == OO_CALC ) tSaveCalcWorkbook(tmpnm);
  return 0;
}

tGetWshCount()
// Количество листов в книге 
{
  int tCount; 
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tGetWshCountExcel(tCount);
  if ( TYPE_OFFICE == OO_CALC ) tGetWshCountCalc(tCount);
  return tCount;
}

tWshVisible(b)
// Функция делает лист либо видимым либо нет
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWshExcelVisible(b);
  if ( TYPE_OFFICE == OO_CALC ) tWshCalcVisible(b);
  return 0;
}

tWriteStringToCell2(i,j,str)
// Запись строки в ячейку, обращение к ячейке по номеру столбца и строки
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tWriteNumToExcelCell2(i,j,str);
  if ( TYPE_OFFICE == OO_CALC ) tWriteNumToCalcCell2(i,j,str);
  return 0;
}

tCopyRowsWsh(wsh1,wsh2,n1,n2,count)
//копирует count строк, начиная со строки n1, с листа wsh1 
//и вставляет их со строки n2, на лист wsh2
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tCopyExcelRowsWsh(wsh1,wsh2,n1,n2,count);
  if ( TYPE_OFFICE == OO_CALC ) tCopyCalcRowsWsh(wsh1-1,wsh2-1,n1-1,n2-1,count);
  return 0;
}

tClearContents(row1,col1,row2,col2,LineS)
// Очистка содержимого, LineS - стиль линий, 1 - очищать, 0 - нет
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tExcelClearContents(row1,col1,row2,col2,LineS);
  if ( TYPE_OFFICE == OO_CALC ) tCalcClearContents(row1-1,col1-1,row2-1,col2-1,LineS);
  return 0;
}

tQuit()
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tExcelQuit();
  if ( TYPE_OFFICE == OO_CALC ) tCalcQuit();
  return 0;
}

tWshCount()
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) return tExcelWshCount();
  if ( TYPE_OFFICE == OO_CALC ) return tCalcWshCount();
  return 0;
}

tRefWshByIndex(i)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) return tExcelRefWshByIndex(i);
  if ( TYPE_OFFICE == OO_CALC ) return tCalcRefWshByIndex(i-1);
  return 0;
}

tDelCol(sheet,n,count)
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tDelExcelCol(sheet,n,count);
  if ( TYPE_OFFICE == OO_CALC ) tDelCalcCol(sheet,n-1,count);
  return 0;
}

tFreeClipboard()
{
  if ( -1 == TYPE_OFFICE ) return -1;
  if ( TYPE_OFFICE == MS_EXCEL ) tFreeExcelClipboard();
  return 0;
}
