How to prevent macro from changing file save state?

I’ve setup a macro on libreoffice calc in python. I have set the macro to run every time the document is opened but I have realised that it changes the save state. That is, if I try to close the file, it asks me to save even if the macro hasn’t made any changes. I’ve made sure to test if the macro is executing setValue but it is not. So, why is libreoffice asking me to save the document despite my macro not making any changes to the document?

My Macro looks something like this:

def fetch(*args):
    model = getModel()
    sheet = model.Sheets['sheetname']

    row = 24
    while len((cell := sheet[f"A{row}"]).String) > 0:
        name = cell.String
        if len(name) < 1:
            row += 1
            continue

        code = MAP[name]
        api_url = f"{BASE_URL}/{code}"

        response = requests.get(api_url).json()
        resp = response['data'][0]['resp'].strip()

        cell2 = sheet[f"C{row}"]

        if cell2.String in resp:
            row += 1
            continue

        try:
            cell2.setValue(float(resp))
        except ValueError:
            break

        row += 1

try to cut down your question to a minimum of essential lines. So comment out all the statements after the first continue. See what happens and so on.
(With an empty sheet and an mri(XSCRIPTCONTEXT.getDocument()), the .modified property is false, with one cell value changed it is true, with CTRL+Z it remains true, though the document visually returns to its empty state.)

At the beginning of any macro call isModified (and save to variable), at the end setModified (to restore).

Sorry. Looks like the issue was on my end. Turns out there’s nothing wrong with the macro itself. My file had some external links to other files (which wasn’t immediately obvious to me) so that was messing with the save state. The earlier comment was right that this would be hard to figure out without the full file. Glad I was able to figure this out on my end.