- Excel Version
- 365
There are at least three ways to automate Excel: VBA, Office Scripts and Script Lab.
This article will show how to use TypeScript and Script Lab to perform repetitive spreadsheet tasks. From the Internet:
*************************************************************************
What is Script Lab?
Wouldn't it be crazy if you could launch Excel, click to open a small code window, and then instantly start writing and executing JavaScript that interacts with your spreadsheet?
Script lab is a tool for anyone who wants to learn about writing Office Add-ins for Excel, Outlook, Word, or PowerPoint. The focus is the Office JavaScript API, which is the technology you need for building Office Add-ins that run across platforms. Maybe you're an experienced Office developer and you want to quickly prototype a feature for your add-in. Or maybe you've never tried writing code for Office and you just want to play with a sample and tweak it to learn more. Either way, Script Lab is for you.
*************************************************************************
Classes are important structures in several programming languages, including JavaScript and TypeScript. The sample code below includes examples on how to:
This article will show how to use TypeScript and Script Lab to perform repetitive spreadsheet tasks. From the Internet:
*************************************************************************
What is Script Lab?
Wouldn't it be crazy if you could launch Excel, click to open a small code window, and then instantly start writing and executing JavaScript that interacts with your spreadsheet?
Script lab is a tool for anyone who wants to learn about writing Office Add-ins for Excel, Outlook, Word, or PowerPoint. The focus is the Office JavaScript API, which is the technology you need for building Office Add-ins that run across platforms. Maybe you're an experienced Office developer and you want to quickly prototype a feature for your add-in. Or maybe you've never tried writing code for Office and you just want to play with a sample and tweak it to learn more. Either way, Script Lab is for you.
*************************************************************************
Classes are important structures in several programming languages, including JavaScript and TypeScript. The sample code below includes examples on how to:
- Extend a class.
- Return a different number of parameters than originally defined
- Use a class to implement an interface
- Declare an abstract class
JavaScript:
$("#run").on("click", () => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const mat = sheet.getRange("b2").getSurroundingRegion()
mat.load("rowCount,values,formulas,address")
await context.sync()
let matv = mat.values
const lastc = mat.getLastCell()
lastc.load("values,address")
await context.sync()
var rn = 10
var fs: string = ""
class adult {
protected sheet2 = sheet
action1() {
sheet.getRange("b" + rn).values = [["Action1: " + lastc.address]]
rn++
lastc.values = matv[1][0]
}
}
class woman extends adult {
public esp = 2
protected p = 3 // class and subclass
protected static readonly vans = matv[3][3]
action2() {
const v: number = matv[4][2]
this.sheet2.getRange("b" + rn).values = [["Action2: " + matv[2][2] + matv[3][2] + v + " at " + this.sheet2.name]]
rn++
this.sheet2.getRange("b" + matv[5][0]).values = [[100]]
}
}
const tc = new woman()
let uni = mat.getResizedRange(-2, -3).getLastCell()
uni.values = [[300]]
sheet.load("values,name")
await context.sync()
matv[1][0] > matv[2][0] ? tc.action1() : tc.action2()
let Lv = matv[1][0]
sheet.getRange("b" + rn).values = [[JSON.stringify(Lv, null, 4)]]
rn++
// *************
class GT {
private vals: number[] = [] // only for this class
addg(...var4: number[]) {
this.vals.push(...var4)
return this.vals.length
}
}
class GC {
countg(s2: string[], one: string) {
return s2.filter(val => val === one).length
}
}
class FC extends GC {
countg2(sa: string[]) {
return super.countg(sa, Lv)
}
}
const fv = new GT()
sheet.getRange("b" + rn).values = [["Row count is " + mat.rowCount]]
rn++
var td: number[] = []
for (let i = 0; i < mat.rowCount; i++) {
td.push(matv[i][2])
}
let av = fv.addg(...td)
sheet.getRange("b" + rn).values = [["length is " + av]]
rn++
const counter = new FC()
td = []
var tds: string[] = []
for (let i = 0; i < mat.rowCount; i++) {
tds.push(matv[i][3])
}
const count = counter.countg2(tds)
sheet.getRange("b" + rn).values = [["count is: " + count]]
rn++
class line {
nocheck: string[]
init(pend: string[]) {
this.nocheck = pend
}
proceed() {
return this.nocheck.pop()
}
}
const acts = new line()
tds = []
for (let i = 0; i < mat.rowCount; i++) {
tds.push(matv[i][4])
}
acts.init(tds)
sheet.getRange("b" + rn).values = [["next is " + acts.proceed()]]
rn++
class SB {
getval() {
return [matv[1][5], matv[0][5]]
}
}
function useSB(veh: SB) {
fs = "getval length is " + veh.getval().length
}
const sbv=new SB
const res=sbv.getval()
//console.log(res.length) // result is 2
useSB({ getval: () => matv[2][5] })
sheet.getRange("b" + rn).values = [[fs]]
rn++
interface child {
name: string; eval(hours: number): void
}
fs = ""
class foreign implements child {
name: string
constructor(name2: string) {
this.name = name2
}
eval(measure: number): void {
for (let i = 0; i < measure; i += 1) {
fs = fs + bel.name + i + "/"
}
}
}
const bel = new foreign(matv[1][6])
bel.eval(2)
sheet.getRange("b" + rn).values = [[fs]]
rn++
abstract class company {
readonly name: string
constructor(name2: string) {
this.name = name2
}
abstract getst(): string[]
}
class mining extends company {
getst(): string[] {
return [matv[2][7]]
}
}
let mys: company
mys = new mining(matv[1][7])
sheet.getRange("b" + rn).values = [[mys.getst() + " / " + mys.name]]
rn++
sheet.getRange("b" + rn).values = [["finished"]]
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
Copy of Script-Lab--a-Microsoft-Garage-project22.xlsx | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | |||
2 | Column1 | Column2 | Column3 | Column4 | Column5 | Column6 | Column7 | Column8 | ||
3 | F | 1 | 5 | A | eat | magic | Zara | magnesium | ||
4 | B | 2 | 6 | C | run | abcdeabcde | Joan | cobalt | ||
5 | 100 | 2 | 7 | F | 300 | America | Tom | cloud | ||
6 | 3 | 22 | 9 | C | jog | Europe | Albert | benchmark | ||
7 | 5 | 33 | 3 | F | clean | Asia | Marina | F | ||
8 | ||||||||||
9 | ||||||||||
10 | Action1: Sheet2!I7 | |||||||||
11 | "F" | |||||||||
12 | Row count is 6 | |||||||||
13 | length is 6 | |||||||||
14 | count is: 2 | |||||||||
15 | next is clean | |||||||||
16 | getval length is 10 | |||||||||
17 | Zara0/Zara1/ | |||||||||
18 | cobalt / magnesium | |||||||||
19 | finished | |||||||||
Sheet2 |