VBA、GAS、pythonの比較

いくつかのプログラミングを書いていると、すぐにこんがらがるので暗記シートを作成

変数の宣言

VBA
 Dim n As Integer
 n = 1
GAS
 const n = 1;
python
 n = 1

配列

VBA
 Dim myArray(1 To 3, 1 To 3) As Integer
  myArray(1, 1) = 1
  myArray(1, 2) = 2
  myArray(1, 3) = 3
  myArray(2, 1) = 4
  myArray(2, 2) = 5
  myArray(2, 3) = 6
  myArray(3, 1) = 7
  myArray(3, 2) = 8
  myArray(3, 3) = 9

GAS
  var myArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
  ];

python
 my_array = [
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
 ]

for

VBA
 Dim myArray(1 To 3, 1 To 3) As Integer
 Dim i As Integer, j As Integer
 Dim num As Integer
  num = 1
  For i = 1 To 3
   For j = 1 To 3
    myArray(i, j) = num
    num = num + 1
   Next j
  Next i

GAS
 var myArray = [];
 var num = 1;
  for (var i = 0; i < 3; i++) {
   myArray[i] = [];
   for (var j = 0; j < 3; j++) {
    myArray[i][j] = num;
    num++;
}
}

python
  my_array = [[j + 1 + i * 3 for j in range(3)] for i in range(3)]

for each

VBA
 Dim myArray(2) As Integer
 Dim i As Integer
  myArray(0) = 1
  myArray(1) = 2
  myArray(2) = 3
  For Each i In myArray
   ’ 各要素に対する処理
   Debug.Print i
  Next i

GAS
 var myArray = [1, 2, 3];
  for (var i of myArray) {
   // 各要素に対する処理
   Logger.log(i);
 }

python
 my_list = [1, 2, 3]
 for item in my_list:
  # 各要素に対する処理
  print(item)

動的配列

VBA
 Dim myArray() As Integer
 ReDim myArray(0 To 2)
 ’ 配列の拡張
 ReDim Preserve myArray(0 To 3)
 myArray(3) = 4
 ’ 配列の削除
 ReDim Preserve myArray(0 To 1)

GAS
 var myArray = [];
 // 要素の追加
 myArray.push(1);
 myArray.push(2);
 myArray.push(3);
 // 一番最後の要素を削除
 myArray.pop();

python
 my_list = []
 # 要素の追加
 my_list.append(1)
 my_list.append(2)
 my_list.append(3)
 # 一番最後の要素を削除
 my_list.pop()

if

VBA
 Dim score As Integer
  score = 75
 If score < 40 Then
  Debug.Print “不合格”
 ElseIf score < 60 Then
  Debug.Print “可”
 ElseIf score < 80 Then
  Debug.Print “良”
 Else
  Debug.Print “優”
End If

GAS
 var score = 75;
 if (score < 40) {
  Logger.log(“不合格”);
 } else if (score < 60) {
  Logger.log(“可”);
 } else if (score < 80) {
  Logger.log(“良”);
 } else {
  Logger.log(“優”);
 }

python
 score = 75
 if score < 40:
  print(“不合格”)
 elif score < 60:
  print(“可”)
 elif score < 80:
  print(“良”)
 else:
  print(“優”)

連想配列(辞書型)

VBA
 Sub VBA_Dictionary()
 Dim dict As Object
  Set dict = CreateObject(“Scripting.Dictionary”)
  ’ 値の追加
  dict.Add “key1”, “value1”
  dict.Add “key2”, “value2”
  ’ 値の取得
  MsgBox dict(“key1”) ‘ value1
  ’ キーの存在チェック
  If dict.Exists(“key2”) Then
  MsgBox “key2 exists”
  End If
 ’ 値の削除
 dict.Remove “key1”
 End Sub

GAS
 function GAS_Object() {
 var dict = {}; // JavaScript のオブジェクトを使用
 // 値の追加
 dict[“key1”] = “value1”;
 dict[“key2”] = “value2”;
 // 値の取得
 Logger.log(dict[“key1”]); // value1
 // キーの存在チェック
 if (“key2” in dict) {
  Logger.log(“key2 exists”);
 }
 // 値の削除
 delete dict[“key1”];
 }

python
 def python_dictionary():
 # 辞書型の作成
 dict = {“key1”: “value1”, “key2”: “value2”}
 # 値の取得
 print(dict[“key1”]) # value1
 # キーの存在チェック
 if “key2” in dict:
  print(“key2 exists”)
 # 値の削除
 del dict[“key1”]

関数

VBA
 Function tashizan(a As Double, b As Double) As Double
  tashizan = a + b
 End Function

GAS
 function tashizan(a, b) {
  return a + b;
 }

python
 def tashizan(a, b):
  return a + b

現在時刻

VBA
 Dim CurrentDateTime as string
 CurrentDateTime = Format(Now(), “”yyyyMMddhhmmss”)

GAS
 var date = new Date();
 var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), “yyyyMMddHHmmss”);

python
 from datetime import datetime
  datetime.now().strftime(“%Y%m%d%H%M%S”)