My problem is to organize data in Excel with the following format: (the square brackets are the datatypes)
(name) - (position),(department)
Post ID [link] | Date [date] | Type of Post [string] | Location [string] | Post Details [string] | Tags Used [string] | Comments [string] | Photos [integer] | Attachments [integer] | Remarks [string]
So far the query that I have is the following:
Post.find_by_sql(["SELECT u.position AS position, u.department AS department, u.first_name AS first_name, u.last_name AS last_name, post.*
FROM apofloors.users u
INNER JOIN
(SELECT p.id, p.user_id, p.created_at, p.post_type, p.geo_lng, p.geo_lat, p.goo_addr1, p.storage_date,
s.msg as details, c.message AS comment,
(SELECT z.name FROM tags z
INNER JOIN post_tags pt ON z.id=pt.tag_id WHERE pt.post_id=p.id) AS tag,
(SELECT COUNT(ph.id) FROM photos ph WHERE ph.post_id=p.id) AS photos_cnt,
(SELECT COUNT(a.id) FROM attachments a WHERE a.post_id=p.id) AS attachments_cnt
FROM posts p
INNER JOIN stories s ON s.post_id=p.id
INNER JOIN comments c ON c.post_id = p.id) post
ON u.id = post.user_id
WHERE (u.id in (?)
AND post.created_at between ? and ?)
ORDER BY post.created_at ASC", users, start_date, end_date])
I have this defined in my controller:
@user_record = []
return unless UserPermission.user_has_permission(current_user.id, 'export_sheet')
unless (params[:start_date]).blank? && (params[:end_date]).blank?
return unless (Time.parse(params[:start_date]) <= Time.parse(params[:end_date]))
@posts = Post.get_sheet(Time.zone.parse(params[:start_date]).to_s, (Time.parse(params[:end_date]) + 1.day).to_s, params[:group_id])
@posts.group_by(&:user_id).each do |id, record|
@user_record.push({
id: id,
name: record.first.first_name + " " + record.first.last_name,
designation: record.first.position + ", " + record.first.department,
posts: record
})
end
@group = Group.find(params[:group_id]).name
respond_to do |format|
# format.xls { send_data @post, :type => 'application/vnd.ms-excel; charset=utf-8; header=present', :filename => 'export.xls' }
format.xls
end
end
In xls.erb, the data will be looped like the following:
<% @user_record.each_with_index do |user| %>
<Row>
<Cell ss:MergeAcross="9" ss:StyleID='szhead'>
<Data ss:Type="String">
<%="#{user[:name]}"-%><%unless (user[:designation]).blank? %><%=" - #{user[:designation]}"-%><%end%>
</Data>
</Cell>
</Row>
<Row>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Post ID</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Date</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Type of Post</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Location</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Post Details</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Tags Used</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Comments</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Photos</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Attachments</Data>
</Cell>
<Cell ss:StyleID="columnheaders">
<Data ss:Type="String">Remarks</Data>
</Cell>
</Row>
<% user[:posts].each do |post| %>
<Row>
<Cell ss:HRef="<%=@APP_URL%>/view-story/<%=post.id%>">
<Data ss:Type="String"><%= post.id %></Data>
</Cell>
<Cell>
<Data ss:Type="String"><%= Time.zone.at(post.created_at).strftime("%B %d, %Y") %></Data>
</Cell>
<Cell>
<Data ss:Type="String"><%= post.post_type %></Data>
</Cell>
<Cell ss:HRef="http://ift.tt/1zZFqE4 post.geo_lat %>,<%= post.geo_lng %>">
<Data ss:Type="String"><%= post.goo_addr1 %></Data>
</Cell>
<Cell ss:StyleID="wrap">
<Data ss:Type="String"><%= post.details %></Data>
</Cell>
<Cell>
<Data ss:Type="String"><%= post.tag %></Data>
</Cell>
<Cell ss:StyleID="wrap">
<Data ss:Type="String"><%= post.comment %></Data>
</Cell>
<Cell>
<Data ss:Type="Number"><%= post.photos_cnt %></Data>
</Cell>
<Cell>
<Data ss:Type="Number"><%= post.attachments_cnt %></Data>
</Cell>
<Cell>
<Data ss:Type="String"></Data>
</Cell>
</Row>
<% end
end %>
No comments:
Post a Comment