简体   繁体   English

Rails Axlsx gem在哪里定义函数

[英]Rails Axlsx gem where to define functions

I'm using the axlsx gem to create excel spreadsheets in my app. 我正在使用axlsx gem在我的应用程序中创建Excel电子表格。 I saw a very useful answer for one of my problems here: 对于这里的一个问题,我看到了一个非常有用的答案:

AXLSX merge cells inside a style AXLSX合并样式内的单元格

I would like to define a function that'll be available for all worksheets but I'm not sure where to put it. 我想定义一个可用于所有工作表的函数,但不确定将其放在何处。

Check out the comment below the answer you have posted: "you can place them anywhere you like as long as you include them in the file you're using them in. For rails if you are using the patch methods you could place them in the initializers folder and then these methods will always be accessible. " 请查看发布的答案下方的评论:“只要将它们包含在使用它们的文件中,就可以将它们放置在所需的任何位置。对于Rails,如果您使用的是补丁方法,则可以将它们放置在初始化文件夹,然后将始终可以访问这些方法。”

Since you want to have this functionality on every single Worksheet, it suggests to place the code inside the config/initializers/ folder of your app. 由于您希望在每个工作表上都具有此功能,因此建议将代码放在应用程序的config / initializers /文件夹中。 The files inside that folder are loaded when your application server starts and placing the code inside a 当您的应用程序服务器启动并将该代码放在

module Axlsx
  class Worksheet 
  end
end

will patch those classes with your new function. 将使用您的新功能修补这些类。 That will allow you to call your function like so: worksheet_object.merge_last_row 这样您就可以像这样调用函数: worksheet_object.merge_last_row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM