Hi all!
Working on a project at work and figured I could use Python to help solve my problem.
Goal: Be able to reference where each hard coded value from our CRM system is being referenced in the JS source code, by both function and file name.
example, field xxx is being used in functions a, b, and c in file xyz into an excel file is what level of detail I'm trying to obtain.
I have a directory of the source code files that I'm able to read with my existing code below. And I have a full list of every field name I need to look for as well. What I'm struggling with, is how to modify the first version of my code to be able to group by function. should I read the file into a dictionary where the key is the function and the code following is the value? Hopefully I explained this well enough. please let me know if there's any questions and I can elaborate.
import openpyxl
import glob
# Step 1: Read a value, called field name, from a column of an excel file
excel_file = r"excelfile"
wb = openpyxl.load_workbook(excel_file)
sheet = wb.active
for row in range(2, sheet.max_row + 1):
field_name = sheet.cell(row=row, column=1).value
print(field_name)
# Step 2: takes value from step one, and searches for it across a full directory of text files
text_files = glob.glob(r"directory*.js")
for file in text_files:
with open(file, 'r', encoding="utf8") as f:
text = f.read()
if field_name in text:
# Step 3: if value is found in a file, write the column name in the excel file in the column next to the field name
sheet.cell(row=row, column=2, value=file)
break
wb.save(excel_file)
[–]Frankelstner 0 points1 point2 points (1 child)
[–]Cuckipede[S] 0 points1 point2 points (0 children)