אני מנסה ליצור משתנה גלובלי בפונקציה מותאמת אישית (UDF) בשיטס.
האפשרויות שמצאתי הם להשתמש ב CacheService כמו כאן:
function GetOfVarGlobal(Key = "_") {
var ValueGlobal = CacheService.getScriptCache();
return ValueGlobal.get(Key);
}
אבל זה רק STRING ואני צריך שיאחסן גם מערכים.
מה הצורך בדיוק?
בקידוד בSHEETS ואקסל יש הרבה פעמים צורך להקצות עמודות לערכי ביניים. אני מנסה להכניס אופציה של יצירת משתנים וכך להפוך את הגליון האלקטרוני ליותר דומה לשפת תכנות.
מה זה מועיל?
- סדר, בעיקר. גם אפשרות לכתוב הערות.
- אופטימיזציה של מקום אחסון. לדוגמה בשיטס אפשר רק עד 2 מליון תאים, שזה עלול להגמר בחישובים מסובכים.
אשמח לדעת
- האם זה יעיל?
- והאם זה אפשרי.
תודה רבה.
מצרף כאן קוד שלי באקסל (באמצע פיתוח - עדיין לא עובד, אבל אפשר להבין את הכיוון. גם אני רוצה להחליף את הCollection במערכים עם ReDim).
אשמח להערות.
Option Explicit
Public Function SubInFunction(ParamArray ReturnOnlyLest() As Variant)
Dim temp As Variant
For Each temp In ReturnOnlyLest
SubInFunction = temp
Next
temp = StaticVars("Remove all temp")
End Function
Public Function SetVar(Value As Variant, Optional Name As String = "") As Variant
' לכתוב למשתנה סטטי = ליצור משתנה או לערוך אותו
' פעולה בלבד - נכתב כפונקציה רק כדי שיהיה אפשר להפעיל את זה דרך פונקציות אקסל
SetVar = StaticVars("Set", Name, Value)
End Function
Public Function GetVar(Optional Name As String = "") As Variant
' לקרוא ממשתנה סטטי = להחזיר את הערך שלו
GetVar = StaticVars("Get", Name)
End Function
Public Function RemoveVar(Optional Name As String = "") As Variant
' למחוק משתנה סטטי
' פעולה בלבד - נכתב כפונקציה רק כדי שיהיה אפשר להפעיל את זה דרך פונקציות אקסל
If Name <> "All" Then
RemoveVar = StaticVars("Remove", Name)
Else
RemoveVar = StaticVars("Remove all")
End If
End Function
Public Function SetTempVar(Value As Variant, Optional Name As String = "") As Variant
' לכתוב למשתנה סטטי = ליצור משתנה או לערוך אותו
' פעולה בלבד - נכתב כפונקציה רק כדי שיהיה אפשר להפעיל את זה דרך פונקציות אקסל
SetTempVar = StaticVars("Set temp", Name, Value)
End Function
Public Function GetTempVar(Optional Name As String = "") As Variant
' לקרוא ממשתנה סטטי = להחזיר את הערך שלו
GetTempVar = StaticVars("Get temp", Name)
End Function
Public Function RemoveTempVar(Optional Name As String = "") As Variant
' למחוק משתנה סטטי
' פעולה בלבד - נכתב כפונקציה רק כדי שיהיה אפשר להפעיל את זה דרך פונקציות אקסל
If Name <> "All" Then
RemoveTempVar = StaticVars("Remove temp", Name)
Else
RemoveTempVar = StaticVars("Remove all temp")
End If
End Function
Private Function StaticVars(Optional ActType As String = "Get", Optional VarName As String = "", Optional Value_ForSetOrForRemove As Variant = "") As Variant
' האחסון וכל הפעולות על משתנים סטטיים
' פונקציה זו היא לפעמים משמשת כשגרה ללא החזרת ערך (רק כך אפשר לעבוד בשפה זו עם משתנים סטטיים)
Static Vars As New Collection
Static VarsName As New Collection
Static TempVars As New Collection
Static TempVarsName As New Collection
Select Case (ActType)
Case "Get"
StaticVars = GetValueInCollectionOfName(VarName, Vars, VarsName)
Case "Set"
SetValueInCollectionOfName Value_ForSetOrForRemove, VarName, Vars, VarsName
StaticVars = 0
Case "Get temp"
StaticVars = GetValueInCollectionOfName(VarName, TempVars, TempVarsName)
Case "Set temp"
SetValueInCollectionOfName Value_ForSetOrForRemove, VarName, TempVars, TempVarsName
StaticVars = 0
Case "Remove"
RemoveValueInCollectionOfName VarName, Vars, VarsName
StaticVars = 0
Case "Remove temp"
RemoveValueInCollectionOfName VarName, TempVars, TempVarsName
StaticVars = 0
Case "Remove all"
RemoveAllValueInCollection Vars
RemoveAllValueInCollection VarsName
StaticVars = 0
Case "Remove all temp"
RemoveAllValueInCollection TempVars
RemoveAllValueInCollection TempVarsName
StaticVars = 0
End Select
End Function
Private Function GetValueInCollectionOfName(NameValue As String, CollectionValues As Collection, CollectionNames As Collection) As Variant
Dim i As Integer
For i = 1 To CollectionNames.Count
If CollectionNames(i) = NameValue Then GetValueInCollectionOfName = CollectionValues.Item(i): Exit Function
Next i
GetValueInCollectionOfName = 0
End Function
Private Sub SetValueInCollectionOfName(Value As Variant, NameValue As String, CollectionValues As Collection, CollectionNames As Collection)
Dim i As Integer
For i = 1 To CollectionNames.Count
If CollectionNames(i) = NameValue Then collection_ApdateValue CollectionValues, i, Value: Exit Sub
Next i
CollectionValues.Add Value
CollectionNames.Add NameValue
End Sub
Private Sub RemoveValueInCollectionOfName(NameValue As String, CollectionValues As Collection, CollectionNames As Collection)
Dim i As Integer
For i = 1 To CollectionNames.Count
If CollectionNames(i) = NameValue Then CollectionValues.Remove i: CollectionNames.Remove i: Exit Sub
Next i
End Sub
Private Sub RemoveAllValueInCollection(MyCollection As Collection)
Dim i As Integer
For i = 1 To MyCollection.Count
MyCollection.Remove i
Next i
End Sub
Private Sub collection_ApdateValue(MyCollection As Collection, Index As Integer, NewValue As Variant)
If MyCollection.Count = Index Then
MyCollection.Add NewValue
MyCollection.Remove Index
Else
MyCollection.Add NewValue, before:=Index + 1
MyCollection.Remove Index
End If
End Sub