Code covered by the BSD License  

Highlights from
Excel popup

Excel popup

by

 

A Simple GUI that pops up a selected sheet of excel.

xlpopup(Excel,Workbook, varargin)
function [handles sheename] = xlpopup(Excel,Workbook, varargin)
vsizeGUI = 100; hsizeGUI = 250;     % size of GUI
sheename = '';
if nargin>2
    if ischar(varargin{1})
        str = varargin{1};
    else
        str = 'xlpopup!';
    end
else
    str = '';
end
%centering the GUI in the window.
screenSize = get(0, 'ScreenSize');
position(1) = (screenSize(3)-hsizeGUI)/2;
position(2) = (screenSize(4)-vsizeGUI)/2;
hGUI = figure(...
    'Visible'        ,'on',...
    'Units'          ,'pixel',...
    'WindowStyle'    ,'normal',...
    'Position'       ,[position(1),position(2)+200,hsizeGUI,vsizeGUI],...
    'MenuBar'        ,'none',...
    'NumberTitle'    ,'off',...
    'resize'         ,'off'...
    );
% the GUI should never become the 'current figure'
%set(hGUI,'HandleVisibility','off');
hpuSheets = uicontrol(hGUI,...
    'Style'          ,'popupmenu',...
    'String'         ,{'Loading...'},...
    'TooltipString'  ,'Select the Sheet to Use in the Excel Workbook',...
    'BackgroundColor',[1 1 1],...
    'Callback'       ,{@puSheets_Callback}...
    );
position = get(hpuSheets, 'position');
position(1) = (hsizeGUI-position(3))/2;
position(2) = (vsizeGUI-position(4))/2;
set(hpuSheets, 'position', [position(1)+50,position(2),position(3)+35,position(4)]);
if nargout > 0
    % collect all handles
    handles = struct('GUI',hGUI,...
        'puSheets',hpuSheets...
        );
end
settitle('Loading...');      % set title of GUI
%update popup menu as soon as the GUI is called.
pause(2);
update();
settitle(str);

    function update()        
        nbSheets = Workbook.Sheets.Count;
        coll = cell(1,nbSheets);
        for s = 1:nbSheets
            coll{s} = Workbook.Sheets.Item(s).Name;
        end
        set(hpuSheets,'String',coll);
        set(hpuSheets,'Value',1);
    end

    function puSheets_Callback(varargin)
        handles.shname = get(hpuSheets, 'Value');
        Activate(Workbook.Sheets.Item(handles.shname));
        sheename = Excel.ActiveSheet.Name;
        Excel.visible = 1;
        % --- check if sheet is empty
        AddrUsedRange = Excel.ActiveSheet.UsedRange.Address;
        %delete the selected sheet if it was empty, then update the popup
        %menu.
        if strcmp(AddrUsedRange,'$A$1') && isnan(Excel.ActiveSheet.UsedRange.Value)
           Excel.Worksheets.Item(handles.shname).Delete;
           update();
        else
            %release GUI.
            delete(hGUI);
        end
    end
    function settitle(str)
        set(hGUI,'Name', str);
    end
%'CloseRequestFcn',{@cleanup}...
end
        

Contact us