前言
公司的项目,需要对项目中事件上报进行重新处理和封装,方便调用和自动上报!自己花了半天时间全部封装搞好,如下图:
其中ASLogsEventName类是存放约定好的事件字段的,但是APP中有四百个事件上报的点,要是一个一个手写上去,估计手要残了,事件Excel列表如下图:
所以肯定不能手写啦😏😉😉
自动化脚本
这里我使用Python脚本,自动读取Excel表格,然后生成iOS的代码,这样就简单的多了!🤗
使用了Python的xlrd库,安装命名如下:
pip3 install xlrd
话不多说上代码如下:
import xlrd
# 过滤了不需要记录的表
_ignore_list = ['更新记录', 'App页面', '公共属性']
# 事件名列表
_event_name_list = []
# 读取excel
def read_excel_table(file_path):
global _tables, _ignore_list
data = xlrd.open_workbook(file_path)
for table_name in data.sheet_names():
if table_name not in _ignore_list:
table = data.sheet_by_name(table_name)
analysis_one_table(table, table_name)
# 分析每个表
def analysis_one_table(table, table_name):
nrows = table.nrows
ncols = table.ncols
# 从1开始 不包括第一行标题
sign = False
for row in range(1, table.nrows):
row_values = table.row_values(row)
if '{' in str(row_values):
sign = True
if sign == False:
analysis_every_row(row_values, table_name)
if '}' in str(row_values):
sign = False
# 分析每一行
def analysis_every_row(row_values, table_name):
global _event_name_list
event_name = row_values[1]
if '(' in event_name:
event_name = event_name[:event_name.find('(')]
if '(' in event_name:
event_name = event_name[:event_name.find('(')]
event_name = ''.join(event_name.split())
event_name = event_name.replace('.', '_')
event_name = event_name.replace('、', '_')
if len(event_name) > 0 and event_name not in _event_name_list:
_event_name_list.append(event_name)
if table_name == '听故事埋点':
notes = row_values[2] + '==' + row_values[3]
else:
notes = row_values[0] + '==' + row_values[2]
notes = notes.replace('\n', ' ')
notes = ''.join(notes.split())
notes = notes.replace('==', ' ')
if notes.startswith(' '):
notes = notes[1:]
pro_str = '/// ' + notes + '\n'
# .h的代码
# pro_str += 'FOUNDATION_EXPORT NSString *const K'+ event_name +';'
# .m的代码
pro_str += 'NSString *const K'+ event_name +' = @"'+ event_name +'";'
print(pro_str)
print('\n')
if __name__ == '__main__':
file_path = 'ahaschoolAppEvent.xlsx'
read_excel_table(file_path)
这样就能自动生成iOS代码了😘注释也自动加上了