No BSD License  

Highlights from
FastFillGrid

image thumbnail
from FastFillGrid by Michael Robbins
Populates a grid, i.e. Microsoft Spreadsheet Object, with a cell array.

FastFillGrid(Spreadsheet1,M,DoPaste,DoWaitbar,MinR,MinC,MaxR,MaxC,AsStr,DoAutoFit)
function FastFillGrid(Spreadsheet1,M,DoPaste,DoWaitbar,MinR,MinC,MaxR,MaxC,AsStr,DoAutoFit)
% FastFillGrid populates a grid, i.e. Microsoft Spreadsheet Object, with a cell array
% The cell array may contain mixed types.
%    FastFillGrid(SPREADSHEET,M) populates SPREADSHEET with M
%    FastFillGrid(SPREADSHEET,M,DOPASTE) usage obsolete
%    FastFillGrid(SPREADSHEET,M,DOPASTE,DOWAITBAR) also uses a progress
%       indicator if DOWAITBAR is 1 (default), does not if DOWAITBAR is 0
%    FastFillGrid(SPREADSHEET,M,DOPASTE,DOWAITBAR,MINR,MINC,MAXR,MAXC) specifies
%       the minimum and maximum rows and cloumns to use
%    FastFillGrid(SPREADSHEET,M,DOPASTE,DOWAITBAR,MINR,MINC,MAXR,MAXC,ASSTR)
%       forces all values to be placed as strings, instead of the default
%       behavior which populates the grid with numbers when possible.
%    FastFillGrid(SPREADSHEET,M,DOPASTE,DOWAITBAR,MINR,MINC,MAXR,MAXC,ASSTR,AUTOFIT)
%       if AUTOFIT = 1, then FastFillGrid will automatically adjust the
%       columnwidth to fit the widest entry.
%
%    Example:
%       figure;
%       spreadsheet = actxcontrol('OWC11.Spreadsheet.11',[0 0 200 200]);
%       M = {'abc', 123; 12.17, logical(1)};
%       FastFillGrid(spreadsheet,M);
%
% IT'S NOT FANCY, BUT IT WORKS
%
%    See also Graph_and_Table, QuestDlgWithGrid, SearchAndReplaceMany,
%       SpreadSheet, DatabaseEditingTool, DuplicateFileFinder,
%       PutCellValue, nn2an
%
%    Keywords: grid spreadsheet ActiveX Active-X Active X GUI Table
%       graph_and_table plot graph table grid object flexgrid
%       msflexgrid ocx tabular

% Michael Robbins
% robbins@bloomberg.net
% michaelrobbins1@yahoo.com

TRUE = 1;
FALSE = 0;
TEST = TRUE;

if nargin<4 DoPaste = TRUE; end;
if nargin<5 DoWaitbar = TRUE; end;
if nargin<6 MinR =   0; end;
if nargin<7 MinC =   0; end;
if nargin<8 MaxR = 9e99; end;
if nargin<9 MaxC = 9e99; end;
if nargin<10 AsStr=0; end;
if nargin<11 DoAutoFit=0; end;

% USE AN ENUMERATED TYPE WORKAROUND
ClassEnumType = {'cell','char','logical','numeric'};

% FIND THE ACTIVE SHEET
ActSheet = get(Spreadsheet1,'ActiveSheet');

% CLEAR CONTENTS
ActSheet.Cells.ClearContents;

% FIND THE ACTIVE CELL, ROW AND COLUMN
% THIS IS NOT NECESSARY, BUT IT IS DONE FOR DEMONSTRATION
ActCell = get(Spreadsheet1,'ActiveCell');
ActCellRow = 1;%get(ActCell,'Row');
ActCellColumn = 1;%get(ActCell,'Column');

if DoWaitbar h = waitbar(0,'Please wait...'); end;
[L,W] = size(M);
MinR = max(MinR,1);
MinC = max(MinC,1);
MaxR = min(L,MaxR);
MaxC = min(W,MaxC);
if DoPaste
    XLfmt = [nn2an(MinR,MinC) ':' nn2an(MaxR,MaxC)];
    Select(Range(Spreadsheet1,XLfmt));
    set(Spreadsheet1.selection,'Value',M);
    Select(Range(Spreadsheet1,nn2an(MinR,MinC)));
    if DoAutoFit
        for c = MinC:MaxC
            invoke(Spreadsheet1.Column,'AutoFit');
        end;
    end;
else
    if DoWaitbar MaxRC = MaxR.*MaxC; end;
    for r = MinR:MaxR
        for c = MinC:MaxC
            if DoWaitbar
                i = c+W.*(r-1);
                waitbar(i./MaxRC,h,sprintf( ...
                    'Posting %d of %d (%d%% done)', ...
                    i,MaxRC,floor(100.*i./MaxRC)));
            end;

            % Select current cell
            XLfmt = nn2an(ActCellRow+r-1,ActCellColumn+c-1);
            Select(Range(ActSheet,XLfmt));

            % Assign value
            ActCell = get(Spreadsheet1,'ActiveCell');
            if PutCellValue(ActCell,M(r,c),AsStr) break; end;
            % Re-select starting cell
            XLfmt = nn2an(ActCellRow,ActCellColumn);
            Select(Range(ActSheet,XLfmt));
            if DoAutoFit && r==MaxR
                invoke(Spreadsheet1.Column,'AutoFit');
            end;
        end;
    end;
end;
if DoWaitbar close(h); end;

% GO BACK TO UPPER LEFT
Select(Range(Spreadsheet1,nn2an(MinR,MinC)));

Contact us at files@mathworks.com