Can't get results in StarBasic Calc macros in Two Scenario , But, I Can get the Results in VBA? Here , are the Codes

Hi Friends,
I Can’t get StarBASIC Macro Result in the Two Scenario

  1. For Next Loop1
  2. For Next Loop2
    But, I can get results in VBA Both Scenario. Here are the Code Sample VBA First and Calc_StarBASIC Codes. Can you help me the mistake, which i done ?

VBA Codes

For Next Loop1

Sub For_Next_Loop1()
      Dim i As Long
      Dim dt As Date
      
      dt = "3/10/2023"    ' yesterDay's Date
      
      For i = 1 To 30
            Range("A" & i).Value = dt + i
      Next i
End Sub

Result

For Next Loop2
==============
Sub For_Next_Loop2()
      Dim i As Long
      
      For i = 1 To 30
            Range("A" & i).Value = Date + i
      Next i
End Sub

Result

Here, are the STAR BASIC codes

For Next Loop1

Dim Doc As Object, Sheets As Object, Sheet As Object

Sub For_Next_Loop1
	  Dim i As Long
	  Dim dt As Date
	  
	  Doc = ThisComponent : Sheets = Doc.Sheets(0)
	  
	  dt = ("3/10/2023")    ' yesterDay's Date
	  
	  For i = 1 To 30 
	  		Sheets.getCellRangeByName("A" & i).Formula = "= dt + i"	
	  Next i 	  
End Sub

Result

For Next Loop2

Sub For_Next_Loop2	  
	  Dim i As Long
	  Doc = ThisComponent : Sheets = Doc.Sheets(1)
	  
	  For i = 1 To 30 
	  		Sheets.getCellRangeByName("A" & i).Formula =  "=Date + i"   ' Cell A1 To A30
	  Next i 	  
End Sub

Result


Sub For_Next_Loop1
Dim Doc As Object, Sheets As Object, Sheet As Object

	  Dim i As Long
	  Dim dt As Date
	  
	  Doc = ThisComponent
	  Sheets = Doc.Sheets(0)
	
      dt = DateValue("2023-10-03")

	  For i = 1 To 30 
	  		Sheets.getCellRangeByName("A" & i).Value = dt +  i	
	  Next i 	  
End Sub


Sub For_Next_Loop2	  
	  Dim i As Long
	  Doc = ThisComponent : Sheets = Doc.Sheets(1)
	  
	  For i = 1 To 30 
	  		Sheets.getCellRangeByName("A" & i).Value =  Date + i 
	  Next i 	  
End Sub

The Formula property is used to insert a formula into the cell, you must insert a value so you must use value, as regards the starting date you must use a function to convert the text into a valid date
See Link
DateValue

1 Like

Hi, @gaetanopr Thanks For the replay and Correcting my mistake and Both Codes Now work and got results

from datetime import date , timedelta as td

def fill_dates(*_):
    doc=XSCRIPTCONTEXT.getDocument()
    cellrange = doc.Sheets[0]["A1:A31"]        
    start = date(2023,10,3)
    cellrange.FormulaArray = [[f"{start+td(days=i):%Y-%m-%d}"] for i in range(31) ]

Version2

from com.sun.star.sheet.FillDirection import TO_BOTTOM
def fill_dates2(*_):
    doc=XSCRIPTCONTEXT.getDocument()
    cellrange = doc.Sheets[0]["A1:A30"]        
    cellrange[0,0].FormulaLocal = "2023-10-3"
    cellrange.fillAuto(TO_BOTTOM, 1)
3 Likes

Thanks @karolus … For Python Code… So, People Will learn…Even me also will learn after 5 to 6 months… This Code Will help …