function dataRange = getSlice(h,thisslice,varargin)

% Slice is specified as a cell array containing a vector of
% indices or logicals. Since a spreasheet is essentially a
% 2d array the slices must be returned as 2d arrays. Consequently,
% there is no difference between {1:10,':',':'} and {1:10), so
% only use the the first element of the "thisslice" cell array
thisslice = thisslice{1};
if min(size(thisslice))>1
   error('Slice out of bounds')
end
if islogical(thisslice)
    thisslice = find(thisslice);
end
if max(thisslice)>(h.EndRow-h.StartRow+1) || min(thisslice)<1
    error('Slice vector out of bounds')
end
minslice = min(thisslice);
thisStartRow = h.StartRow+minslice-1;
thisEndRow = h.StartRow+max(thisslice)-1;
thisslice = thisslice-minslice+1;

% Argument checking
cols = floor(h.Column);
if isempty(cols)
    dataRange = [];
    return
end
if any(cols<1)
    error('Column positions must be greater than 0')
end
[cols J] = sort(cols);

% Parse column vector into contiguous intervals and extract by block
k = 1;
dataRange = {};
while k+1<=length(cols)
  lastCol = cols(k);
  while k+1<=length(cols) && cols(k+1)-cols(k)==1
      k = k+1;
  end
  cellblock = localParseColNumbers(lastCol,cols(k),thisStartRow,thisEndRow);
  r = h.comhandle.Range(cellblock);
  dataRange = [dataRange get(r,'Value')];   
  k =  k+1;
end

% If there is one isolated column remaining
if k==length(cols)
  cellblock = localParseColNumbers(cols(k),cols(k),thisStartRow,thisEndRow);
  r = get(h.comhandle.Range(cellblock),'Value');
  if ~iscell(r)
      r = {r};
  end
  dataRange = [dataRange r];   
end

% Remap data back to original column and rows
dataRange = dataRange(thisslice,J);

% Special rules for interpreting time vectors
if nargin==3 && isa(varargin{1},'hds.variable') && strcmpi(varargin{1}.Name,'Time') 
    if length(cols) ~=1 
        error('Excel data sources used for tiem vectors must consist of a single column')
    end
    Idouble = cellfun('isclass',dataRange,'double');
    % TO DO: Need to check when times are char but interval is too long
    % so there are trailing NaNs
    if all(cellfun('isclass',dataRange,'char'))
        % Verify the stirngs are dates
        try
            datestr(dataRange);
        catch
            error('One or more timestamps cannot be parsed as a date')
        end
    elseif length(Idouble)==length(dataRange)
        if any(isnan(cell2mat(dataRange(Idouble))))
            error('Time vectors can have no blank cells')
        end
    else
        error('Time vectors cannot mix strings and numeric time representations')
    end
else  % Numeric vector
	% Convert strings to numeric NaNs
	dataRange(~cellfun('isclass',dataRange,'double')) = {NaN}; 
	dataRange = cell2mat(dataRange);
end

    
function cellblock = localParseColNumbers(startcol,endcol,startrow,endrow)

% Parses column and row vertices of a rectangle into the form 'XX#:YY#'

startcolstr = char('A'+rem(startcol,26)-1);
if startcol>26
   startcolstr = [char(floor(startcol/26)-1+'A')  startcolstr];
end
endcolstr = char('A'+rem(endcol,26)-1);
if endcol>26
   endcolstr = [char(floor(endcol/26)-1+'A')  endcolstr];
end
cellblock = [startcolstr sprintf('%d',startrow) ':' endcolstr sprintf('%d',endrow)];








