![](https://take-web.com/task_help/wp-content/uploads/2021/04/macro_compare-27.png)
![](http://take-web.com/task_help/wp-content/themes/cocoon-master/images/man.png)
表計算ソフトは、Excelが有名ですが値段が高すぎます。無料で使えるLibreOffice CalcとGoogleシートが便利です。
セル(A1)に値代入、削除
![](http://take-web.com/brog/wp-content/uploads/cell.gif)
Excel コード
ボタンを押すと「おはよう」と入力し、次にボタン押すとそれを消します。
Range(“セルの番地”) = ” 文字列 ” または 数値 ’// 数値は””いらない
Sub 練習1() If Range("A1") = "" Then Range("A1") = "おはよう!" Else Range("A1") = "" End If End Sub
「数値+文字」””なしだとエラーになります。 コンパイルエラー
![数値文字列エラー](https://take-web.com/task_help/wp-content/uploads/2021/04/excel_err_string_27.png)
LibreOffice Calcのコード
上記のExcelファイルを開く
自動で一行目に「Option VBASupport 1」が追加されます。
ボタンに関連づけられてるマクロ消えるので、再登録してください。
![マクロ再登録](https://take-web.com/task_help/wp-content/uploads/2021/04/button_re_set-27.png)
LibreのBasicを使って書くと
長い! エディタに補完機能もない! 面倒!
わたしは、秀丸の補完機能を使いながら書いています。
Sub Main Doc=ThisComponent MySheet = Doc.CurrentController.ActiveSheet Cel = MySheet.getCellRangeByName("A1") If Cel.String = "" Then Cel.String = "日本最高!!" Else Cel.String = "" End If End Sub
LibreOffice Calc 文字列と数値明確に分けます .string .value
GAS セルに値を入れる
![](http://take-web.com/brog/wp-content/uploads/2019-9-13_19-12-4_No-00.png)
function myFunction() { var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheet = spreadsheet.getSheetByName("シート1"); var cell = sheet.getRange(1, 1); // 1つのセル if (cell.getValue() === ""){ cell.setValue("安倍さんを応援する"); } else { cell.setValue(""); } }
AutoitでExcelを操作する
![](http://take-web.com/task_help/wp-content/themes/cocoon-master/images/man.png)
AutoitはBasicのような文法で入門しやすいと思います。PCの自動化、画像認識、GUIとユーザーさんが作ってくれた関数を利用するといろいろなことができます。
![](http://take-web.com/brog/wp-content/uploads/2019-9-14_7-1-30_No-00.png)
#include "Excel.au3" $oExcel = _Excel_Open() ;存在してるExcelに接続 $oExcel.Sheets("Sheet2").Select If $oExcel.Cells(1,1).value = "" Then $oExcel.Cells(1,1).value = "立憲民主党嫌い" Else $oExcel.Range("A1").value = "" EndIf
AutoitでCalc(LibreOffice)を操作する
![](http://take-web.com/brog/wp-content/uploads/2019-9-14_11-21-30_No-00.png)
#include "OOoCalc.au3" $oCalc = _OOoCalc_BookAttach("A1_autoit.ods") If _OOoCalc_ReadCell($oCalc,"A1") = "" Then _OOoCalc_WriteCell($oCalc,"日本は素晴らしい",0,0) ;cell(x,y) Else _OOoCalc_WriteCell($oCalc,"",0,0) ;cell(x,y) EndIf
コメント