Realtime Excel.WriteToFile in a Function

I’m having a bit of a problem writing my Dynamo script.

For a manufacturer, I’m creating a module to place insulation panels on a roof. This works well enough to be used now.
I’m using just a small handful of nodes to extract the necessary parameters, and two functions to calculate a few new ones. It is able to create a paneldivision and export it to Revit in less than ten seconds for a few 4 by 6 meter sample roofs. It’s all dandy to have that visualised, but in the end, what the manufacturer needs is how many panels exist in each row.

That’s where the Excel.WriteToFile function comes in ofcourse.

This is the function that creates the panels, and to reduce the code required this is also where I want to write the data to Excel.

Points is a list of startpoints, lines is a list of numbers indicating each row’s length, angle is a list of numbers (from angle in degrees), vector is a list of vectors, file is the reference to the Excel file, roofNumber is used to create a new sheet in the file.

Below function creates surfaces, checks if they coincide with the roof, cuts them to size if necessary, and all the surfaces are added to a list using index p which resets to 0 for each new row.
So what I’m trying to do is use the Excel.WriteToFile function to write the value of p for each row to a new line, in a new sheet for each roof surface (which is handled by the string roofNumber). I’m unsuccessful in writing the p values to their designated rows. So what I’m looking for is some help in determining the following:

  • where do I place the WriteToFile command?
  • Where do I increment the row number?

I’ve tried different options that seem straightforward, but to no avail. Any help would be greatly appreciated.

def GeneratePanels(points, lines, angle, roof, vector, file, roofNumber)
return = [Imperative]
for(point in points)
for(line in lines)
i = 0;
p = 0;
while(i * 600 < Math.Abs(line))
path = Line.ByStartPointDirectionLength(point,
perpVec, plaatbreedte);
profile = Line.ByStartPointDirectionLength
(point, vector, plaatlengte);

surface = Surface.BySweep(path, profile);

segments = surface.Split(roof);

for(segment in segments)
surface = segment;
surfaces[p] = surface;
p = p + 1;
point = path.EndPoint;

i = i + 1;
return = surfaces;

I moved on from this. Basically the WriteToFile functionality appears to be only able to write formatted data sets. I can’t just write one number, then move to the next row, and write the next number to the next row within a For loop or so it seems. Instead, I processed the generated panels list and then write the whole dataset in one go.