1*b1cdbd2cSJim Jagielski /* Licensed to the Apache Software Foundation (ASF) under one 2*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 3*b1cdbd2cSJim Jagielski * distributed with this work for additional information 4*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 5*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 6*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 7*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 8*b1cdbd2cSJim Jagielski * 9*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 12*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 13*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 15*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 16*b1cdbd2cSJim Jagielski * under the License. 17*b1cdbd2cSJim Jagielski * 18*b1cdbd2cSJim Jagielski *************************************************************/ 19*b1cdbd2cSJim Jagielski 20*b1cdbd2cSJim Jagielski package fvt.uno.sc.sheet; 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski import static org.junit.Assert.*; 23*b1cdbd2cSJim Jagielski import org.junit.After; 24*b1cdbd2cSJim Jagielski import org.junit.AfterClass; 25*b1cdbd2cSJim Jagielski import org.junit.Before; 26*b1cdbd2cSJim Jagielski import org.junit.BeforeClass; 27*b1cdbd2cSJim Jagielski import org.junit.Test; 28*b1cdbd2cSJim Jagielski import org.openoffice.test.common.Testspace; 29*b1cdbd2cSJim Jagielski import org.openoffice.test.uno.UnoApp; 30*b1cdbd2cSJim Jagielski import testlib.uno.SCUtil; 31*b1cdbd2cSJim Jagielski import com.sun.star.beans.XPropertySet; 32*b1cdbd2cSJim Jagielski import com.sun.star.container.XIndexAccess; 33*b1cdbd2cSJim Jagielski import com.sun.star.lang.XComponent; 34*b1cdbd2cSJim Jagielski import com.sun.star.sheet.SheetLinkMode; 35*b1cdbd2cSJim Jagielski import com.sun.star.sheet.XSheetLinkable; 36*b1cdbd2cSJim Jagielski import com.sun.star.sheet.XSpreadsheet; 37*b1cdbd2cSJim Jagielski import com.sun.star.sheet.XSpreadsheetDocument; 38*b1cdbd2cSJim Jagielski import com.sun.star.sheet.XSpreadsheets; 39*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime; 40*b1cdbd2cSJim Jagielski import com.sun.star.util.XRefreshable; 41*b1cdbd2cSJim Jagielski 42*b1cdbd2cSJim Jagielski /** 43*b1cdbd2cSJim Jagielski * Basic sheet operator testing 44*b1cdbd2cSJim Jagielski * 45*b1cdbd2cSJim Jagielski */ 46*b1cdbd2cSJim Jagielski public class SheetBasicTest { 47*b1cdbd2cSJim Jagielski UnoApp unoApp = new UnoApp(); 48*b1cdbd2cSJim Jagielski XSpreadsheetDocument scDocument = null; 49*b1cdbd2cSJim Jagielski XComponent scComponent = null; 50*b1cdbd2cSJim Jagielski 51*b1cdbd2cSJim Jagielski @BeforeClass setUpBeforeClass()52*b1cdbd2cSJim Jagielski public static void setUpBeforeClass() throws Exception { 53*b1cdbd2cSJim Jagielski 54*b1cdbd2cSJim Jagielski } 55*b1cdbd2cSJim Jagielski 56*b1cdbd2cSJim Jagielski @AfterClass tearDownAfterClass()57*b1cdbd2cSJim Jagielski public static void tearDownAfterClass() throws Exception { 58*b1cdbd2cSJim Jagielski } 59*b1cdbd2cSJim Jagielski 60*b1cdbd2cSJim Jagielski @Before setUp()61*b1cdbd2cSJim Jagielski public void setUp() throws Exception { 62*b1cdbd2cSJim Jagielski unoApp.start(); 63*b1cdbd2cSJim Jagielski // New a SC document 64*b1cdbd2cSJim Jagielski scComponent = unoApp.newDocument("scalc"); 65*b1cdbd2cSJim Jagielski } 66*b1cdbd2cSJim Jagielski 67*b1cdbd2cSJim Jagielski @After tearDown()68*b1cdbd2cSJim Jagielski public void tearDown() throws Exception { 69*b1cdbd2cSJim Jagielski unoApp.closeDocument(scComponent); 70*b1cdbd2cSJim Jagielski unoApp.close(); 71*b1cdbd2cSJim Jagielski } 72*b1cdbd2cSJim Jagielski 73*b1cdbd2cSJim Jagielski /** 74*b1cdbd2cSJim Jagielski * test insert a sheet, rename sheet name and delete sheet 75*b1cdbd2cSJim Jagielski */ 76*b1cdbd2cSJim Jagielski @Test insertRenameDeleteSheet()77*b1cdbd2cSJim Jagielski public void insertRenameDeleteSheet() throws Exception { 78*b1cdbd2cSJim Jagielski // Insert a sheet named aa after first sheet 79*b1cdbd2cSJim Jagielski String sheetname = "aa"; 80*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 81*b1cdbd2cSJim Jagielski XSpreadsheets spreadsheets = scDocument.getSheets(); 82*b1cdbd2cSJim Jagielski spreadsheets.insertNewByName(sheetname, (short) 1); 83*b1cdbd2cSJim Jagielski 84*b1cdbd2cSJim Jagielski // active the sheet second sheet aa 85*b1cdbd2cSJim Jagielski XSpreadsheet newSpreadSheet = SCUtil.getSCSheetByIndex(scDocument, 86*b1cdbd2cSJim Jagielski (short) 1); 87*b1cdbd2cSJim Jagielski SCUtil.setCurrentSheet(scDocument, newSpreadSheet); 88*b1cdbd2cSJim Jagielski 89*b1cdbd2cSJim Jagielski // get the new speadsheet name 90*b1cdbd2cSJim Jagielski assertEquals("actual should equals aa", sheetname, 91*b1cdbd2cSJim Jagielski SCUtil.getSCSheetNameByIndex(scDocument, (short) 1)); 92*b1cdbd2cSJim Jagielski 93*b1cdbd2cSJim Jagielski // Change the Spreadsheet name 94*b1cdbd2cSJim Jagielski String changedname = "SpeadsheetAfterChange"; 95*b1cdbd2cSJim Jagielski SCUtil.setSCSheetNameByIndex(scDocument, (short) 1, changedname); 96*b1cdbd2cSJim Jagielski 97*b1cdbd2cSJim Jagielski // Save and reload document 98*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "TestSpreadsheet", "ods"); 99*b1cdbd2cSJim Jagielski XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp, 100*b1cdbd2cSJim Jagielski scDocument, "TestSpreadsheet.ods"); 101*b1cdbd2cSJim Jagielski 102*b1cdbd2cSJim Jagielski scDocument = scDocumentTemp; 103*b1cdbd2cSJim Jagielski String sheetnameaftermove = SCUtil.getSCSheetNameByIndex(scDocument, 104*b1cdbd2cSJim Jagielski (short) 1); 105*b1cdbd2cSJim Jagielski 106*b1cdbd2cSJim Jagielski // Verify the changed Spreadsheet name 107*b1cdbd2cSJim Jagielski assertEquals("actual should equals SpeadsheetAfterChange", changedname, 108*b1cdbd2cSJim Jagielski sheetnameaftermove); 109*b1cdbd2cSJim Jagielski 110*b1cdbd2cSJim Jagielski scDocument.getSheets().removeByName(changedname); 111*b1cdbd2cSJim Jagielski 112*b1cdbd2cSJim Jagielski assertFalse("actual should equals false", 113*b1cdbd2cSJim Jagielski spreadsheets.hasByName(changedname)); 114*b1cdbd2cSJim Jagielski SCUtil.save(scDocumentTemp); 115*b1cdbd2cSJim Jagielski } 116*b1cdbd2cSJim Jagielski 117*b1cdbd2cSJim Jagielski /** 118*b1cdbd2cSJim Jagielski * Test copy and past sheet 119*b1cdbd2cSJim Jagielski */ 120*b1cdbd2cSJim Jagielski @Test copypastesheet()121*b1cdbd2cSJim Jagielski public void copypastesheet() throws Exception { 122*b1cdbd2cSJim Jagielski // Insert some value into cells 123*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 124*b1cdbd2cSJim Jagielski String souceSheetName = "sourcesheet"; 125*b1cdbd2cSJim Jagielski SCUtil.setSCSheetNameByIndex(scDocument, (short) 0, souceSheetName); 126*b1cdbd2cSJim Jagielski String[][] stringValues = { { "", "Jan", "Feb", "Mar", "Apr", "Mai" }, 127*b1cdbd2cSJim Jagielski { "Profit", "12.3", "43.2", "5.1", "76", "56.8" }, 128*b1cdbd2cSJim Jagielski { "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" }, }; 129*b1cdbd2cSJim Jagielski XSpreadsheet sourceSpreadSheet = SCUtil.getSCSheetByName(scDocument, 130*b1cdbd2cSJim Jagielski souceSheetName); 131*b1cdbd2cSJim Jagielski // input strings into sheet1 132*b1cdbd2cSJim Jagielski SCUtil.setTextToCellRange(sourceSpreadSheet, 0, 0, stringValues); 133*b1cdbd2cSJim Jagielski // copy the sheet from sourcesheet to copysheet 134*b1cdbd2cSJim Jagielski String newcopysheet = "copysheet"; 135*b1cdbd2cSJim Jagielski XSpreadsheets spreadsheets = scDocument.getSheets(); 136*b1cdbd2cSJim Jagielski spreadsheets.copyByName(souceSheetName, newcopysheet, (short) 2); 137*b1cdbd2cSJim Jagielski 138*b1cdbd2cSJim Jagielski // Save and reload document 139*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "TestCopysheet", "xls"); 140*b1cdbd2cSJim Jagielski XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp, 141*b1cdbd2cSJim Jagielski scDocument, "TestCopysheet.xls"); 142*b1cdbd2cSJim Jagielski scDocument = scDocumentTemp; 143*b1cdbd2cSJim Jagielski 144*b1cdbd2cSJim Jagielski XSpreadsheet copysheet = SCUtil 145*b1cdbd2cSJim Jagielski .getSCSheetByIndex(scDocument, (short) 2); 146*b1cdbd2cSJim Jagielski String[][] CopystringValues = SCUtil.getTextFromCellRange(copysheet, 0, 147*b1cdbd2cSJim Jagielski 0, 5, 2); 148*b1cdbd2cSJim Jagielski assertArrayEquals("Expect string value should be stringValues", 149*b1cdbd2cSJim Jagielski stringValues, CopystringValues); 150*b1cdbd2cSJim Jagielski 151*b1cdbd2cSJim Jagielski } 152*b1cdbd2cSJim Jagielski 153*b1cdbd2cSJim Jagielski /** 154*b1cdbd2cSJim Jagielski * Test move sheet 155*b1cdbd2cSJim Jagielski */ 156*b1cdbd2cSJim Jagielski @Test movesheet()157*b1cdbd2cSJim Jagielski public void movesheet() throws Exception { 158*b1cdbd2cSJim Jagielski 159*b1cdbd2cSJim Jagielski // new sc document 160*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 161*b1cdbd2cSJim Jagielski XSpreadsheets spreadsheets = scDocument.getSheets(); 162*b1cdbd2cSJim Jagielski 163*b1cdbd2cSJim Jagielski // change the first sheet name and input same value into the sheet cell 164*b1cdbd2cSJim Jagielski String sheetname = "sourcesheet"; 165*b1cdbd2cSJim Jagielski SCUtil.setSCSheetNameByIndex(scDocument, (short) 0, sheetname); 166*b1cdbd2cSJim Jagielski String[][] stringValues = { { "", "Jan", "Feb", "Mar", "Apr", "Mai" }, 167*b1cdbd2cSJim Jagielski { "Profit", "12.3", "43.2", "5.1", "76", "56.8" }, 168*b1cdbd2cSJim Jagielski { "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" }, }; 169*b1cdbd2cSJim Jagielski XSpreadsheet movesheet = SCUtil 170*b1cdbd2cSJim Jagielski .getSCSheetByIndex(scDocument, (short) 0); 171*b1cdbd2cSJim Jagielski SCUtil.setTextToCellRange(movesheet, 0, 0,stringValues); 172*b1cdbd2cSJim Jagielski 173*b1cdbd2cSJim Jagielski // Before move, get the 2nd sheet name 174*b1cdbd2cSJim Jagielski String secondSheetNameBeforeMove = SCUtil.getSCSheetNameByIndex( 175*b1cdbd2cSJim Jagielski scDocument, (short) 1); 176*b1cdbd2cSJim Jagielski 177*b1cdbd2cSJim Jagielski // move the first sheet 178*b1cdbd2cSJim Jagielski spreadsheets.moveByName(sheetname, (short) 2); 179*b1cdbd2cSJim Jagielski 180*b1cdbd2cSJim Jagielski // Save and reload document 181*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "Testmovesheet", "xls"); 182*b1cdbd2cSJim Jagielski XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp, 183*b1cdbd2cSJim Jagielski scDocument, "Testmovesheet.xls"); 184*b1cdbd2cSJim Jagielski scDocument = scDocumentTemp; 185*b1cdbd2cSJim Jagielski 186*b1cdbd2cSJim Jagielski // After move, get the first sheet name, and verify it same as 2nd sheet 187*b1cdbd2cSJim Jagielski // name before move 188*b1cdbd2cSJim Jagielski String firstsheetnameAfterMove = SCUtil.getSCSheetNameByIndex( 189*b1cdbd2cSJim Jagielski scDocument, (short) 0); 190*b1cdbd2cSJim Jagielski assertEquals("Expect result should be Sheet2", 191*b1cdbd2cSJim Jagielski secondSheetNameBeforeMove, firstsheetnameAfterMove); 192*b1cdbd2cSJim Jagielski 193*b1cdbd2cSJim Jagielski // Get the target sheet name after move 194*b1cdbd2cSJim Jagielski String sheetnameAfterMove = SCUtil.getSCSheetNameByIndex(scDocument, 195*b1cdbd2cSJim Jagielski (short) 1); 196*b1cdbd2cSJim Jagielski assertEquals("Expect result should be sourcesheet", sheetname, 197*b1cdbd2cSJim Jagielski sheetnameAfterMove); 198*b1cdbd2cSJim Jagielski 199*b1cdbd2cSJim Jagielski // Check the cell value after move 200*b1cdbd2cSJim Jagielski XSpreadsheet sheetaftermove = SCUtil.getSCSheetByIndex(scDocument, 201*b1cdbd2cSJim Jagielski (short) 1); 202*b1cdbd2cSJim Jagielski String[][] stringValuesaftermove = SCUtil.getTextFromCellRange( 203*b1cdbd2cSJim Jagielski sheetaftermove, 0, 0, 5, 2); 204*b1cdbd2cSJim Jagielski 205*b1cdbd2cSJim Jagielski assertArrayEquals("Expect result should be stringValues", stringValues, 206*b1cdbd2cSJim Jagielski stringValuesaftermove); 207*b1cdbd2cSJim Jagielski } 208*b1cdbd2cSJim Jagielski 209*b1cdbd2cSJim Jagielski /** 210*b1cdbd2cSJim Jagielski * Test hide and show sheet 211*b1cdbd2cSJim Jagielski */ 212*b1cdbd2cSJim Jagielski @Test hideShowSheet()213*b1cdbd2cSJim Jagielski public void hideShowSheet() throws Exception { 214*b1cdbd2cSJim Jagielski // Insert a sheet named hide sheet after first sheet 215*b1cdbd2cSJim Jagielski String sheetname = "hide sheet"; 216*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 217*b1cdbd2cSJim Jagielski XSpreadsheets spreadsheets = scDocument.getSheets(); 218*b1cdbd2cSJim Jagielski spreadsheets.insertNewByName(sheetname, (short) 1); 219*b1cdbd2cSJim Jagielski 220*b1cdbd2cSJim Jagielski // active the sheet second sheet "hide sheet" 221*b1cdbd2cSJim Jagielski XSpreadsheet secondSpreadSheet = SCUtil.getSCSheetByIndex(scDocument, 222*b1cdbd2cSJim Jagielski (short) 1); 223*b1cdbd2cSJim Jagielski SCUtil.setCurrentSheet(scDocument, secondSpreadSheet); 224*b1cdbd2cSJim Jagielski // get second sheet name and verify it should be "hide sheet" 225*b1cdbd2cSJim Jagielski assertEquals("expect active sheet name will be hide sheet", sheetname, 226*b1cdbd2cSJim Jagielski SCUtil.getSCSheetNameByIndex(scDocument, (short) 1)); 227*b1cdbd2cSJim Jagielski 228*b1cdbd2cSJim Jagielski // hide the sheet you insert 229*b1cdbd2cSJim Jagielski XPropertySet sheetPropertySet = (XPropertySet) UnoRuntime 230*b1cdbd2cSJim Jagielski .queryInterface(XPropertySet.class, secondSpreadSheet); 231*b1cdbd2cSJim Jagielski boolean isvisiable = false; 232*b1cdbd2cSJim Jagielski sheetPropertySet.setPropertyValue("IsVisible", isvisiable); 233*b1cdbd2cSJim Jagielski 234*b1cdbd2cSJim Jagielski // Save and reload document 235*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "Testhideshowsheet", "xls"); 236*b1cdbd2cSJim Jagielski XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp, 237*b1cdbd2cSJim Jagielski scDocument, "Testhideshowsheet.xls"); 238*b1cdbd2cSJim Jagielski scDocument = scDocumentTemp; 239*b1cdbd2cSJim Jagielski 240*b1cdbd2cSJim Jagielski // get the active sheet name after hide sheet, it should be Sheet2 241*b1cdbd2cSJim Jagielski String sheet2Name = SCUtil.getSCSheetNameByIndex(scDocument, (short) 2); 242*b1cdbd2cSJim Jagielski String activesheetname = SCUtil.getSCActiveSheetName(scDocument); 243*b1cdbd2cSJim Jagielski assertEquals("Expect sheet name should be Sheet2", sheet2Name, 244*b1cdbd2cSJim Jagielski activesheetname); 245*b1cdbd2cSJim Jagielski 246*b1cdbd2cSJim Jagielski // show sheet "hide sheet" 247*b1cdbd2cSJim Jagielski sheetPropertySet = (XPropertySet) UnoRuntime.queryInterface( 248*b1cdbd2cSJim Jagielski XPropertySet.class, 249*b1cdbd2cSJim Jagielski SCUtil.getSCSheetByIndex(scDocument, (short) 1)); 250*b1cdbd2cSJim Jagielski isvisiable = true; 251*b1cdbd2cSJim Jagielski sheetPropertySet.setPropertyValue("IsVisible", isvisiable); 252*b1cdbd2cSJim Jagielski 253*b1cdbd2cSJim Jagielski // active sheet "hide sheet" 254*b1cdbd2cSJim Jagielski secondSpreadSheet = SCUtil.getSCSheetByIndex(scDocument, (short) 1); 255*b1cdbd2cSJim Jagielski SCUtil.setCurrentSheet(scDocument, secondSpreadSheet); 256*b1cdbd2cSJim Jagielski 257*b1cdbd2cSJim Jagielski // Get current active sheet name, verify it same as "hide sheet" 258*b1cdbd2cSJim Jagielski String currentactivesheetname = SCUtil.getSCActiveSheetName(scDocument); 259*b1cdbd2cSJim Jagielski assertEquals("Expect active sheet name is hidesheet", sheetname, 260*b1cdbd2cSJim Jagielski currentactivesheetname); 261*b1cdbd2cSJim Jagielski SCUtil.save(scDocument); 262*b1cdbd2cSJim Jagielski } 263*b1cdbd2cSJim Jagielski 264*b1cdbd2cSJim Jagielski /** 265*b1cdbd2cSJim Jagielski * Test sheet tab color 266*b1cdbd2cSJim Jagielski */ 267*b1cdbd2cSJim Jagielski @Test sheetColor()268*b1cdbd2cSJim Jagielski public void sheetColor() throws Exception { 269*b1cdbd2cSJim Jagielski // get first sheet propertyset 270*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 271*b1cdbd2cSJim Jagielski XSpreadsheets spreadsheets = scDocument.getSheets(); 272*b1cdbd2cSJim Jagielski XSpreadsheet firstSpreadSheet = SCUtil.getSCSheetByIndex(scDocument, 273*b1cdbd2cSJim Jagielski (short) 0); 274*b1cdbd2cSJim Jagielski XPropertySet sheet1PropertySet = (XPropertySet) UnoRuntime 275*b1cdbd2cSJim Jagielski .queryInterface(XPropertySet.class, firstSpreadSheet); 276*b1cdbd2cSJim Jagielski 277*b1cdbd2cSJim Jagielski // Set sheet tab color to 111 278*b1cdbd2cSJim Jagielski sheet1PropertySet.setPropertyValue("TabColor", 111); 279*b1cdbd2cSJim Jagielski 280*b1cdbd2cSJim Jagielski // copy the color sheet to new sheet 281*b1cdbd2cSJim Jagielski spreadsheets.copyByName( 282*b1cdbd2cSJim Jagielski SCUtil.getSCSheetNameByIndex(scDocument, (short) 0), 283*b1cdbd2cSJim Jagielski "newsheet", (short) 3); 284*b1cdbd2cSJim Jagielski 285*b1cdbd2cSJim Jagielski // Save and reopen the document 286*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "Testcolorsheet", "ods"); 287*b1cdbd2cSJim Jagielski XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp, 288*b1cdbd2cSJim Jagielski scDocument, "Testcolorsheet.ods"); 289*b1cdbd2cSJim Jagielski scDocument = scDocumentTemp; 290*b1cdbd2cSJim Jagielski 291*b1cdbd2cSJim Jagielski // Get first sheet color 292*b1cdbd2cSJim Jagielski sheet1PropertySet = (XPropertySet) UnoRuntime.queryInterface( 293*b1cdbd2cSJim Jagielski XPropertySet.class, 294*b1cdbd2cSJim Jagielski SCUtil.getSCSheetByIndex(scDocument, (short) 0)); 295*b1cdbd2cSJim Jagielski int firstSheetcolorid = (Integer) sheet1PropertySet 296*b1cdbd2cSJim Jagielski .getPropertyValue("TabColor"); 297*b1cdbd2cSJim Jagielski 298*b1cdbd2cSJim Jagielski // Get the copyed sheet color 299*b1cdbd2cSJim Jagielski XPropertySet newsheetPropertySet = (XPropertySet) UnoRuntime 300*b1cdbd2cSJim Jagielski .queryInterface(XPropertySet.class, 301*b1cdbd2cSJim Jagielski SCUtil.getSCSheetByIndex(scDocument, (short) 3)); 302*b1cdbd2cSJim Jagielski int copySheetcolorid = (Integer) newsheetPropertySet 303*b1cdbd2cSJim Jagielski .getPropertyValue("TabColor"); 304*b1cdbd2cSJim Jagielski 305*b1cdbd2cSJim Jagielski // Verify first sheet color changed successfully 306*b1cdbd2cSJim Jagielski assertEquals("Expect color should be 111", 111, firstSheetcolorid); 307*b1cdbd2cSJim Jagielski 308*b1cdbd2cSJim Jagielski // Verify first sheet color same as copy sheet color 309*b1cdbd2cSJim Jagielski assertEquals("Expect color should be 111", firstSheetcolorid, 310*b1cdbd2cSJim Jagielski copySheetcolorid); 311*b1cdbd2cSJim Jagielski } 312*b1cdbd2cSJim Jagielski 313*b1cdbd2cSJim Jagielski /** 314*b1cdbd2cSJim Jagielski * test insert sheet from other file 315*b1cdbd2cSJim Jagielski */ 316*b1cdbd2cSJim Jagielski @Test insertSheetFromfile()317*b1cdbd2cSJim Jagielski public void insertSheetFromfile() throws Exception { 318*b1cdbd2cSJim Jagielski // New a document source.xls, add value to 3 sheet 319*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 320*b1cdbd2cSJim Jagielski XSpreadsheets spreadsheets = scDocument.getSheets(); 321*b1cdbd2cSJim Jagielski XSpreadsheet firstSheet = SCUtil.getSCSheetByIndex(scDocument, 322*b1cdbd2cSJim Jagielski (short) 0); 323*b1cdbd2cSJim Jagielski XSpreadsheet secondSheet = SCUtil.getSCSheetByIndex(scDocument, 324*b1cdbd2cSJim Jagielski (short) 1); 325*b1cdbd2cSJim Jagielski XSpreadsheet thirdSheet = SCUtil.getSCSheetByIndex(scDocument, 326*b1cdbd2cSJim Jagielski (short) 2); 327*b1cdbd2cSJim Jagielski SCUtil.setFormulaToCell(firstSheet, 1, 2, "=2*2"); 328*b1cdbd2cSJim Jagielski SCUtil.setFormulaToCell(secondSheet, 1, 2, "=2*2"); 329*b1cdbd2cSJim Jagielski SCUtil.setFormulaToCell(thirdSheet, 1, 2, "=2*2"); 330*b1cdbd2cSJim Jagielski 331*b1cdbd2cSJim Jagielski // Save and close this document 332*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "source", "xls"); 333*b1cdbd2cSJim Jagielski SCUtil.closeFile(scDocument); 334*b1cdbd2cSJim Jagielski 335*b1cdbd2cSJim Jagielski // get source document URL 336*b1cdbd2cSJim Jagielski String SourcestoreUrl = Testspace.getUrl("output/sc/" + "source" + "." 337*b1cdbd2cSJim Jagielski + "xls"); 338*b1cdbd2cSJim Jagielski 339*b1cdbd2cSJim Jagielski // New a document 340*b1cdbd2cSJim Jagielski scComponent = unoApp.newDocument("scalc"); 341*b1cdbd2cSJim Jagielski scDocument = SCUtil.getSCDocument(scComponent); 342*b1cdbd2cSJim Jagielski spreadsheets = scDocument.getSheets(); 343*b1cdbd2cSJim Jagielski // Insert firstexternalsheet sheet, link with Sheet1 in source document 344*b1cdbd2cSJim Jagielski // and the link mode is NORMAL 345*b1cdbd2cSJim Jagielski spreadsheets.insertNewByName("firstexternalsheet", (short) 3); 346*b1cdbd2cSJim Jagielski XSpreadsheet firstexternalsheet = SCUtil.getSCSheetByIndex(scDocument, 347*b1cdbd2cSJim Jagielski (short) 3); 348*b1cdbd2cSJim Jagielski XSheetLinkable xfirstSheetLinkable = (XSheetLinkable) UnoRuntime 349*b1cdbd2cSJim Jagielski .queryInterface(XSheetLinkable.class, firstexternalsheet); 350*b1cdbd2cSJim Jagielski xfirstSheetLinkable.link(SourcestoreUrl, "", "MS Excel 97", "", 351*b1cdbd2cSJim Jagielski SheetLinkMode.NORMAL); 352*b1cdbd2cSJim Jagielski 353*b1cdbd2cSJim Jagielski // Insert secondexternalsheet sheet, link with Sheet2 in source document 354*b1cdbd2cSJim Jagielski // and the link mode is VALUE 355*b1cdbd2cSJim Jagielski spreadsheets.insertNewByName("secondexternalsheet", (short) 4); 356*b1cdbd2cSJim Jagielski XSpreadsheet secondexternalsheet = SCUtil.getSCSheetByIndex(scDocument, 357*b1cdbd2cSJim Jagielski (short) 4); 358*b1cdbd2cSJim Jagielski XSheetLinkable xsecondSheetLinkable = (XSheetLinkable) UnoRuntime 359*b1cdbd2cSJim Jagielski .queryInterface(XSheetLinkable.class, secondexternalsheet); 360*b1cdbd2cSJim Jagielski xsecondSheetLinkable.link(SourcestoreUrl, "Sheet2", "MS Excel 97", "", 361*b1cdbd2cSJim Jagielski SheetLinkMode.VALUE); 362*b1cdbd2cSJim Jagielski 363*b1cdbd2cSJim Jagielski // Insert secondexternalsheet sheet, link with Sheet2 in source document 364*b1cdbd2cSJim Jagielski // and the link mode is NONE 365*b1cdbd2cSJim Jagielski spreadsheets.insertNewByName("thirdexternalsheet", (short) 5); 366*b1cdbd2cSJim Jagielski XSpreadsheet thirdexternalsheet = SCUtil.getSCSheetByIndex(scDocument, 367*b1cdbd2cSJim Jagielski (short) 5); 368*b1cdbd2cSJim Jagielski XSheetLinkable xthirdSheetLinkable = (XSheetLinkable) UnoRuntime 369*b1cdbd2cSJim Jagielski .queryInterface(XSheetLinkable.class, thirdexternalsheet); 370*b1cdbd2cSJim Jagielski xthirdSheetLinkable.link(SourcestoreUrl, "Sheet3", "MS Excel 97", "", 371*b1cdbd2cSJim Jagielski SheetLinkMode.NONE); 372*b1cdbd2cSJim Jagielski 373*b1cdbd2cSJim Jagielski // Verify firstexternalsheet 374*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be =2*2", "=2*2", 375*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(firstexternalsheet, 1, 2)); 376*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be 4", "4", 377*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(firstexternalsheet, 1, 2)); 378*b1cdbd2cSJim Jagielski 379*b1cdbd2cSJim Jagielski // Verify secondexternalsheet 380*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be 4", "4", 381*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(secondexternalsheet, 1, 2)); 382*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be 4", "4", 383*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(secondexternalsheet, 1, 2)); 384*b1cdbd2cSJim Jagielski 385*b1cdbd2cSJim Jagielski // Verify thirdexternalsheet 386*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be blank", "", 387*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(thirdexternalsheet, 1, 2)); 388*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be blank", "", 389*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(thirdexternalsheet, 1, 2)); 390*b1cdbd2cSJim Jagielski 391*b1cdbd2cSJim Jagielski // save document and verify the linked sheet again 392*b1cdbd2cSJim Jagielski SCUtil.saveFileAs(scComponent, "linked", "ods"); 393*b1cdbd2cSJim Jagielski XSpreadsheetDocument tempscDocument = SCUtil.reloadFile(unoApp, 394*b1cdbd2cSJim Jagielski scDocument, "linked.ods"); 395*b1cdbd2cSJim Jagielski scDocument = tempscDocument; 396*b1cdbd2cSJim Jagielski firstexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 3); 397*b1cdbd2cSJim Jagielski secondexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 4); 398*b1cdbd2cSJim Jagielski thirdexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 5); 399*b1cdbd2cSJim Jagielski 400*b1cdbd2cSJim Jagielski // Verify firstexternalsheet 401*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be =2*2", "=2*2", 402*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(firstexternalsheet, 1, 2)); 403*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be 4", "4", 404*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(firstexternalsheet, 1, 2)); 405*b1cdbd2cSJim Jagielski 406*b1cdbd2cSJim Jagielski // Verify secondexternalsheet 407*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be 4", "4", 408*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(secondexternalsheet, 1, 2)); 409*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be 4", "4", 410*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(secondexternalsheet, 1, 2)); 411*b1cdbd2cSJim Jagielski 412*b1cdbd2cSJim Jagielski // Verify thirdexternalsheet 413*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be blank", "", 414*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(thirdexternalsheet, 1, 2)); 415*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be blank", "", 416*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(thirdexternalsheet, 1, 2)); 417*b1cdbd2cSJim Jagielski 418*b1cdbd2cSJim Jagielski //save and close document 419*b1cdbd2cSJim Jagielski SCUtil.save(scDocument); 420*b1cdbd2cSJim Jagielski SCUtil.closeFile(scDocument); 421*b1cdbd2cSJim Jagielski 422*b1cdbd2cSJim Jagielski //Open souce document and change the value in souce document 423*b1cdbd2cSJim Jagielski XSpreadsheetDocument sourcescDocument = SCUtil.reloadFile(unoApp, 424*b1cdbd2cSJim Jagielski scDocument, "source.xls"); 425*b1cdbd2cSJim Jagielski firstSheet = SCUtil.getSCSheetByIndex(sourcescDocument, (short) 0); 426*b1cdbd2cSJim Jagielski secondSheet = SCUtil.getSCSheetByIndex(sourcescDocument, (short) 1); 427*b1cdbd2cSJim Jagielski thirdSheet = SCUtil.getSCSheetByIndex(sourcescDocument, (short) 2); 428*b1cdbd2cSJim Jagielski SCUtil.setFormulaToCell(firstSheet, 1, 2, "=3*3"); 429*b1cdbd2cSJim Jagielski SCUtil.setFormulaToCell(secondSheet, 1, 2, "=3*3"); 430*b1cdbd2cSJim Jagielski SCUtil.setFormulaToCell(thirdSheet, 1, 2, "=3*3"); 431*b1cdbd2cSJim Jagielski SCUtil.save(sourcescDocument); 432*b1cdbd2cSJim Jagielski SCUtil.closeFile(sourcescDocument); 433*b1cdbd2cSJim Jagielski 434*b1cdbd2cSJim Jagielski //Open link document 435*b1cdbd2cSJim Jagielski tempscDocument = SCUtil.reloadFile(unoApp, scDocument, "linked.ods"); 436*b1cdbd2cSJim Jagielski scDocument = tempscDocument; 437*b1cdbd2cSJim Jagielski spreadsheets = scDocument.getSheets(); 438*b1cdbd2cSJim Jagielski 439*b1cdbd2cSJim Jagielski firstexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 3); 440*b1cdbd2cSJim Jagielski secondexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 4); 441*b1cdbd2cSJim Jagielski thirdexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 5); 442*b1cdbd2cSJim Jagielski 443*b1cdbd2cSJim Jagielski //get Object SheetLinks for document 444*b1cdbd2cSJim Jagielski XPropertySet sheetpropertyset = (XPropertySet) UnoRuntime 445*b1cdbd2cSJim Jagielski .queryInterface(XPropertySet.class, scDocument); 446*b1cdbd2cSJim Jagielski Object sheetLinks = sheetpropertyset.getPropertyValue("SheetLinks"); 447*b1cdbd2cSJim Jagielski 448*b1cdbd2cSJim Jagielski XIndexAccess xsheetlinks = (XIndexAccess) UnoRuntime.queryInterface( 449*b1cdbd2cSJim Jagielski XIndexAccess.class, sheetLinks); 450*b1cdbd2cSJim Jagielski 451*b1cdbd2cSJim Jagielski //Refresh all links 452*b1cdbd2cSJim Jagielski for (int i = 0; i < xsheetlinks.getCount(); i++) { 453*b1cdbd2cSJim Jagielski Object sheetlink = xsheetlinks.getByIndex(i); 454*b1cdbd2cSJim Jagielski XRefreshable xsheetRefreshable = (XRefreshable) UnoRuntime 455*b1cdbd2cSJim Jagielski .queryInterface(XRefreshable.class, sheetlink); 456*b1cdbd2cSJim Jagielski xsheetRefreshable.refresh(); 457*b1cdbd2cSJim Jagielski } 458*b1cdbd2cSJim Jagielski 459*b1cdbd2cSJim Jagielski // Verify firstexternalsheet 460*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be =3*3", "=3*3", 461*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(firstexternalsheet, 1, 2)); 462*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be 9", "9", 463*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(firstexternalsheet, 1, 2)); 464*b1cdbd2cSJim Jagielski 465*b1cdbd2cSJim Jagielski // Verify secondexternalsheet 466*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be 9", "9", 467*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(secondexternalsheet, 1, 2)); 468*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be 9", "9", 469*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(secondexternalsheet, 1, 2)); 470*b1cdbd2cSJim Jagielski 471*b1cdbd2cSJim Jagielski // Verify thirdexternalsheet 472*b1cdbd2cSJim Jagielski assertEquals("Expect formula should be blank", "", 473*b1cdbd2cSJim Jagielski SCUtil.getFormulaFromCell(thirdexternalsheet, 1, 2)); 474*b1cdbd2cSJim Jagielski assertEquals("Expect formula result should be blank", "", 475*b1cdbd2cSJim Jagielski SCUtil.getTextFromCell(thirdexternalsheet, 1, 2)); 476*b1cdbd2cSJim Jagielski 477*b1cdbd2cSJim Jagielski //Save the document before close 478*b1cdbd2cSJim Jagielski SCUtil.save(scDocument); 479*b1cdbd2cSJim Jagielski 480*b1cdbd2cSJim Jagielski } 481*b1cdbd2cSJim Jagielski 482*b1cdbd2cSJim Jagielski } 483