time | Calls | line |
---|
| | 1 | function book = createWorkbook(format, filename, interop, sheet, sheetNamesOnly)
|
| | 2 | %CREATEWORKBOOK Create a Workbook object
|
| | 3 | %
|
| | 4 | % BOOK = CREATEWORKBOOK(FMT) creates a new book from the format
|
| | 5 | % specified by FMT.
|
| | 6 | %
|
| | 7 | % BOOK = CREATEWORKBOOK(FMT, FILENAME) creates a book from the file
|
| | 8 | % specified by FILENAME. FMT must match the format of the spreadsheet
|
| | 9 | % specified by FILENAME.
|
| | 10 | %
|
| | 11 | % BOOK = CREATEWORKBOOK(..., true|false) an optional third input argument
|
| | 12 | % specifies whether to use an interactive application to instantiate the Workbook.
|
| | 13 | %
|
| | 14 | % BOOK = CREATEWORKBOOK(..., sheet) an optional fourth input argument
|
| | 15 | % specifies to sheet to read from the Workbook.
|
| | 16 | %
|
| | 17 | % BOOK = CREATEWORKBOOK(..., sheetNames) an optional fifth logical input
|
| | 18 | % argument that specifies that only the sheet names need to be returned
|
| | 19 | %
|
| | 20 | % Example interactive workbook:
|
| | 21 | % book = matlab.io.spreadsheet.internal.Workbook('xlsx','airlinesmall_subset.xlsx', false, 1);
|
| | 22 | %
|
| | 23 | % See also matlab.io.spreadsheet.internal.Book,
|
| | 24 | % matlab.io.spreadsheet.internal.Sheet
|
| | 25 | %
|
| | 26 |
|
| | 27 | % Copyright 2015-2019 The MathWorks, Inc.
|
| | 28 |
|
< 0.001 | 2 | 29 | narginchk(1, 5);
|
< 0.001 | 2 | 30 | nargoutchk(0, 1);
|
| | 31 |
|
< 0.001 | 2 | 32 | if nargin < 2
|
| | 33 | filename = [];
|
| 2 | 34 | end
|
< 0.001 | 2 | 35 | if nargin < 3 || isempty(interop)
|
| | 36 | interop = false;
|
| 2 | 37 | end
|
< 0.001 | 2 | 38 | if nargin < 4
|
| | 39 | sheet = [];
|
< 0.001 | 2 | 40 | end
|
< 0.001 | 2 | 41 | if nargin < 5
|
< 0.001 | 2 | 42 | sheetNamesOnly = false;
|
| 2 | 43 | end
|
< 0.001 | 2 | 44 | initLibrary();
|
| | 45 |
|
< 0.001 | 2 | 46 | isODSorXLSB = contains(format, {'ods', 'xlsb'}, 'IgnoreCase',true);
|
| | 47 | % Use COM on Windows if UseExcel i.e. interop is true or input file format is ods / xlsb
|
< 0.001 | 2 | 48 | tryInterop = ispc && (interop || isODSorXLSB);
|
< 0.001 | 2 | 49 | if interop && isWebServerCheck
|
| | 50 | warning(message("MATLAB:spreadsheet:book:webserviceExcelWarn"));
|
< 0.001 | 2 | 51 | end
|
| | 52 |
|
< 0.001 | 2 | 53 | if(interop && ~tryInterop)
|
| | 54 | oldState = warning('off','backtrace');
|
| | 55 | % Warn on Non-Windows platforms if 'UseExcel' is set to true & silently switching to LibXL
|
| | 56 | warning(message('MATLAB:spreadsheet:book:noExcel'));
|
| | 57 | warning(oldState);
|
< 0.001 | 2 | 58 | end
|
| | 59 |
|
| | 60 | % determine whether sheet should be set to empty -
|
| | 61 | % 1) if filename was not passed in
|
| | 62 | % 2) if sheet was not passed in
|
| | 63 | % 3) if sheet was passed in as a char/string
|
| | 64 | % 4) if sheet was passed in as a numeric vector
|
< 0.001 | 2 | 65 | emptySheet = (nargin == 4 && isempty(sheet));
|
< 0.001 | 2 | 66 | emptyFile = isempty(filename);
|
< 0.001 | 2 | 67 | numericScalarCheck = ~emptySheet && isnumeric(sheet) && numel(sheet) > 1;
|
< 0.001 | 2 | 68 | stringCheck = ~emptySheet && (isstring(sheet) || ischar(sheet));
|
< 0.001 | 2 | 69 | comMode = tryInterop == 1;
|
< 0.001 | 2 | 70 | if emptyFile || emptySheet || numericScalarCheck || stringCheck || comMode
|
| | 71 | sheet = [];
|
< 0.001 | 2 | 72 | end
|
| | 73 |
|
< 0.001 | 2 | 74 | if isODSorXLSB
|
| | 75 | % this optimization is not possible for Windows
|
| | 76 | sheetNamesOnly = [];
|
< 0.001 | 2 | 77 | end
|
| | 78 |
|
< 0.001 | 2 | 79 | try %#ok<TRYNC>
|
0.008 | 2 | 80 | book = constructWorkbook(format, filename, tryInterop, sheet, sheetNamesOnly);
|
< 0.001 | 2 | 81 | return;
|
| | 82 | end
|
| | 83 | try
|
| | 84 | book = constructWorkbook(format, filename, false, sheet, sheetNamesOnly);
|
| | 85 | return;
|
| | 86 | catch ME
|
| | 87 | if ~tryInterop
|
| | 88 | if strcmp(ME.identifier, 'MATLAB:spreadsheet:book:unsupportedFormat')
|
| | 89 | switch format
|
| | 90 | case {'ods', 'xlsb'}
|
| | 91 | error(message('MATLAB:spreadsheet:book:fileTypeUnsupported', format));
|
| | 92 | case {'csv'}
|
| | 93 | invalidFormatError(ispc);
|
| | 94 | otherwise
|
| | 95 | rethrow(ME);
|
| | 96 | end
|
| | 97 | elseif strcmp(ME.identifier, 'MATLAB:spreadsheet:book:invalidFormat')
|
| | 98 | invalidFormatError(ispc);
|
| | 99 | end
|
| | 100 | end
|
| | 101 | end
|
| | 102 | rethrow(ME);
|
| | 103 | end
|
Other subfunctions in this file are not included in this listing.