File: __COM_spsh_close__.m

package info (click to toggle)
octave-io 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: objc: 2,092; cpp: 546; python: 438; makefile: 204; xml: 23; sh: 20
file content (125 lines) | stat: -rw-r--r-- 4,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
## Copyright (C) 2012-2025 Philip Nienhuis
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## __COM_spsh_close__ - internal function: close a spreadsheet file using COM

## Author: Philip Nienhuis <prnienhuis@users.sf.net>
## Created: 2012-10-12

function [ xls ] = __COM_spsh_close__ (xls)

    ## If file has been changed, write it out to disk.
    ##
    ## Note: COM / VB supports other Excel file formats as FileFormatNum
    ## or xlFileFormat (see below switch stmt):
    ## (see Excel Help, VB reference, Enumerations, xlFileType)

    ## xls.changed = 0: no changes: just close;
    ##               1: existing file with changes: save, close.
    ##               2: new file with data added: save, close
    ##               3: new file, no added added (empty): close & delete on disk

    xls.app.Application.DisplayAlerts = 0;
    try
      if (xls.changed > 0 && xls.changed < 3)
        if (isfield (xls, "nfilename"))
          fname = xls.nfilename;
        else
          fname = xls.filename;
        endif
        fname = make_absolute_filename (strsplit (fname, filesep){end});
        if (xls.changed == 2)
          ## Probably a newly created, or renamed, Excel file. Get proper format
          [~, ~, ext] = fileparts (fname);
          ## https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat
          switch ext
            case ".txt"
              ## Current Platform Text
              xlFileFormat = -4158;
            case {".wks"}
              ## Lotus 1-2-3 format (general) / MS Works
              xlFileFormat = 4;
            case {".wk1"}
              ## Lotus 1-2-3 format (general)
              xlFileFormat = 5;
            case ".csv"
              ## CSV (general, not platform-specific)
              xlFileFormat = 6;
            case ".dbf"
              ## dBase 3 format (xDbf3). Note; xlDBf2 = 7, xlDBF4 = 11
              xlFileFormat = 8;
            case ".dif"
              ## Data Interchange format
              xlFileFormat = 9;
            case ".wk3"
              ## Lotus-1-2-3
              xlFileFormat = 15;
            case ".wq1"
              ## Quattro Pro format
              xlFileFormat = 34;
            case ".prn"
              ## Printer Text
              xlFileFormat = 36;
            case ".wk4"
              ## Lotus 1-2-3
              xlFileFormat = 38;
            case {".htm", ".html"}
              ## HTML format
              xlFileFormat = 44;
            case {".mht", ".mhtml"}
              ## Web Archive
              xlFileFormat = 45;
            case ".xml"
              ## XML spreadsheet 2003
              xlFileFormat = 46;
            case "xlsb"
              ## Excel Binary Workbook
              xlFileFormat = 50;
            case ".xlsx"
              ## Open XML Workbook
              xlFileFormat = 51;
            case ".xlsm"
              ## Open XML Workbook Macro Enabled
              xlFileFormat = 52;
            case ".xls"
              ## Excel 97-2003 Workbook
              xlFileFormat = 56;
            case ".pdf"
              ## Printer Text
              xlFileFormat = 57;
            case ".ods"
              ## Open Document Format
              xlFileFormat = 60;
            otherwise
              ## Fall back to OpenXML .xlsx
              xlFileFormat = 51;
          endswitch
          xls.workbook.SaveAs (fname, xlFileFormat);
        elseif (xls.changed == 1)
          ## Just updated existing Excel file
          xls.workbook.Save ();
        endif
        xls.changed = 0;
        xls.workbook.Close (fname);
      endif
      xls.app.Quit ();
      delete (xls.workbook);  ## This statement actually closes the workbook
      delete (xls.app);       ## This statement actually closes down Excel
    catch
      xls.app.Application.DisplayAlerts = 1;
    end_try_catch

endfunction