1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

覚え書き(3) - 数式を取得する -

Posted at

覚え書き(3)

数式を取得する

R1C1形式での取得

以下は、セル「C3」に入力された数式を取得し、隣のセル「D3」に取得した数式を文字列として代入する。

Suusiki_1.js
var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = true;              //Excelを表示する
var Book = Excel.Workbooks.Open("エクセルファイル名");    //既存のブックを読み込む
var Sheet = Book.Worksheets(1);     //1シート目のオブジェクトを取得

Sheet.Cells(3, 4).Value = "'" + Sheet.Cells(3, 3).FormulaR1C1;    //Cell(3,3)="C3"の式を、Cell(3,4)="D3"へ代入

WScript.Quit();

数式をそのまま他のセルへ代入すると、数式として解釈されるので、明示的に文字列とするために「'」(シングルクオーテーション)を付加している。

A1形式での取得

以下は、数式をA1形式で取得する。

Suusiki_2.js
var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = true;              //Excelを表示する
var Book = Excel.Workbooks.Open("エクセルファイル名");    //既存のブックを読み込む
var Sheet = Book.Worksheets(1);     //1シート目のオブジェクトを取得

Sheet.Range("D3").Value = "'" + Sheet.Range("C3").Formula;

WScript.Quit();

セルA3とB3の値を足し算した結果をセルC3に代入する式(C3=A3+B3)の場合は、下記のとおり
R1C1形式:=RC[-2]+RC[-1]
A1形式 :=A3+B3

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?