def Simulate(): global num_of_sim global op global num_of_inp global iter_num global current_inp global inpcell global output_flag global usr_selection global inparraycell global formstr global arrayvals global simcomplete global inp xlcCalculateNow() xlcCalculateDocument() if not output_flag: xlcAlert("Select a output cell. If selected, click reload") return # checkMIArray=False win32com.client.gencache.Rebuild() xl_window = get_active_object() xl_app = win32com.client.Dispatch(xl_window).Application xl_app.Calculation=xlCalculationManual # xl_app = win32com.client.GetActiveObject("Excel.Application") # win32com.client.gencache.EnsureDispatch(xl_app) num_of_sim = 0 if ((inpcell is not None) and formstr == str(inpcell.formula) ): checkMIArray=True # xlcAlert('inside looop:'+ str(inp).strip('[]')) rect = inpcell.rect # xlcAlert(inpcell.address) # xlcAlert(str(rect.first_row)) # xlcAlert(str(rect.first_col)) selection = xl_app.ActiveSheet.Cells(int(rect.first_row)+1,int(rect.first_col)+1) # selection.value = 100 # inpcell.value = 100 # xlcAlert("click OK") app1 = QApplication(sys.argv) form = Dialog.popup() app1.exec_() num_of_sim = int(Dialog.retval()) usr_selection = str(Dialog.retsel()) writeFG = Dialog.writeflag() if num_of_sim > 0: current_inp =1 # xlcAlert(str(num_of_sim)) start = time.time() op=None op = zeros((num_of_inp,num_of_sim)) xl_app.ScreenUpdating = False xl_app.DisplayStatusBar = False xl_app.EnableEvents = False for j in range(num_of_inp): current_inp = j if (checkMIArray): selection.Value = inp[current_inp] for i in range(num_of_sim): iter_num = i xlcCalculateDocument() end = time.time() xl_app.ScreenUpdating = True xl_app.DisplayStatusBar = True xl_app.EnableEvents = True current_inp = 0 simcomplete= True # selection.value = inp[current_inp] # selection.Formula = inpcell.formula if checkMIArray: fstr = '=MiInput(' + inparraycell.address + ')' selection.Formula = fstr # xlcAlert(str(checkMIArray)) if not checkMIArray: inp = [0] UI.draw(op, inp,usr_selection) # Store data in a CSV format if checkMIArray: popupstr = '' for idx,inpvalue in enumerate(inp): tupleop = None tupleop = tuple(op[idx]) datastr = "data" + str(idx) xl_app.ActiveWorkbook.Names.Add(datastr,tupleop,False) popupstr = popupstr + "Output variable for Input " + str(inpvalue) + "is: "+str(datastr) + "\n" popupstr = popupstr + "You can use all the excel statistical functions on these variables" xlcAlert(popupstr) else: tupleop = tuple(op) xl_app.ActiveWorkbook.Names.Add("data",tupleop,False) xlcAlert("Your Output Variable is 'data'" + "\n" + "You can use all the excel statistical functions on this variable") if writeFG: config = get_config() config.read('pyxll.cfg') dir_path = config.get('LOG','path') xlcAlert("Data stored at "+str(dir_path)) file_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S") if checkMIArray: # xlcAlert(str(len(op[1]))) for idx,inpvalue in enumerate(inp): file_name1 = file_name +"-input "+str(inpvalue) +'.csv' if os.path.exists(dir_path): myfile = open(os.path.join(dir_path, file_name1), 'wb') wr = csv.writer(myfile, dialect='excel') wr.writerow(op[idx]) else: if os.path.exists(dir_path): file_name = file_name + '.csv' myfile = open(os.path.join(dir_path, file_name), 'wb') wr = csv.writer(myfile, dialect='excel') wr.writerows(op)