Notes On Programming in Excel (Typescript Included)
Notes On Programming in Excel (Typescript Included)
- Conditional (if/else)
- Loops
If (operation == “multiply”) {
x * y;
} else {
x*y
If (x>y){
if(temp<=5){
console.log(“It’s cold!”)
} else {
console.log(“It’s cold!”)
First if is accomplished, 6 is between 3 & 9. But not the second so nothing will happen.
If we are not in this period, do nothing, or if we are in this period and the temperature is greater than 5
do nothing, else say it’s cold.
sum += i;
console.log(sum);
1+2+3+4+5+6+7+8+9+10=55
For(let i=1;i<=10;i++){
If (i %% 2 == 0){
Sum+= i;
2+4+6+8+10=30
Let i = 0
Let sum =0
Sum += i
i++
0+1+2+3+4=10
To build a function:
Function build_name(fistname,lastname){
Console.log(build_name(“Bob”,”Addams”))
Function factorial(n){
If (n==1){
Return 1
}else{
Return factorial(n-1) * n
Factorial(5)
Sería:
Factorial(4)*5
Factorial(3)*4*5
Factorial(2)*3*4*5
1*2*3*4*5
120
1- Write TS code
2- Transpile to JS
3- Run
Function add(x,y) {
Return x + y
Console.log(add(3,4))
Console.log(add(3,”4”))
Function add(x:number,y:number){
Return x + y
Console.log(add(3,4))
That will give us errors.
- String
- Number
- Boolean
Union types is a type formed from other types and represents values which may be any of the
constituent types.
To reduce time we can just put a new name for the combination
Let sum = 0
Console.log(sum)
I menor que once tells the loop to continue while that is true
I++ tells the loop to increment the counter I by 1 each time it executes the loop body.
Return x % y == 0
To see the output don’t forget to put the output shower, console.log
Console.log(is_divisible(18,3));
The code an object contains come in the form of methods (special type of functions which can also use
the data contained in the object)
Scripts are written in typescript and can be created, edited and run with the built-in code editor
Office scripts include (preloaded) an excelscript library which allows you to interact with the excel
workbook.
An excel script must have a main function, with a workbook object as its argument
Function main(workbook: ExcelScript.Workbook) {
// Do things here
Console.log(sheet.getName());
In
// Do things here
greeting: string;
this.greeting = message
greet() {
Console.log(greeter.greet())
Now it should say “Hello class” because what is changing is the message associated.
selectedSheet.getRange(“B6”).setValue(“Hello World”);
// Get sheet
//Get table
table.addColumn(-1).setName(“Total sales”);
//Number of cols/rows
Let nc = table.getColumns().length
Let nm = nc – 5;
Let nr = table.getRowCount();
Table.convertToRange();
//Set formula
Ts_1.setFormulasR1C1(“=sum(RC[-“ + nm + “];RC[-1])”);
//Autofit
Sheet.getRangeByIndexes(0,nc-1,nr,1).getFormat().autofitColumns();
//Get table
table.addColumn(-1).setName(“Total sales”);
//Number of cols/rows
Let nc = table.getColumns().length
Let nm = nc – 5;
Let nr = table.getRowCount();
Lo primero son las columnas, lo segundo es la posición -5 de las columnas para poner algo ahí, lo tercero
es el número de filas.
Table.convertToRange();
Vale, aquí lo que está pasando es que estamos seleccionando la primera celda de total sales (porque al
ser tabla solo necesitamos esa). 1 es el número de row donde queremos cogerlo, es la dos porque hay
headers pero es 1 porque es 0 index. Nc-1 es porque empezamos por la última columna, pero el
programa es 0 index así que hay que restar uno. Nr para que se aplique en todas las rows. 1 porque es
una sola columna.
//Set formula
Ts_1.setFormulasR1C1(“=sum(RC[-“ + nm + “];RC[-1])”);
//Get sheet
//Get table
let nc = table.getColumns().length
let nm = nc -4;
pTable.addRowHierarchy(pTable.getHierarchy(“Gender”));
ptalbe.addRowHierarchy(pTable.getHierarchy(“Size”));
pTable.addDataHierarchy(ptable.getHierarchy(name));
sizeField.applyFilter({
manualFilter: {
});
Volvemos a la clase
Two constructs which are often used in ExcelScripts and we have not seen yet
1. Enums: Every type as a constant and you can access it, for instance all the type of charts there
are. Collections of named constants. They store all these identifiers.
2. Interfaces
Collection of variables, each with a given type
Interface Person {
Name : string;
Age : number
}
The question is if it is greater or equal to zero. Green and bold if that’s the case.
Get with the type string will return the object if it exists with the name you’ve given it.
sheet1.setName(“Pivot”);
let pt = create_pivot(workbook);
change_to_average(pt);
barChart = addBarChart(Workbook.getWorksheet(“Pivot”));
pt.addRowHierarchy(pt.getHierarchy(“Gender”));
pt.addDataHierarchy(pt.getHierarchy(“Sales-January”));
return pt
datas[i].setSummarizeBy(ExcelScript.AggregationFunction.average);
return BarChart;
chart.getLegend().setVisible(false);
chart.setLeft(
´
function main(workbook: ExcelScript.Workbook)
// Get sheet
//Get table
table.addColumn(-1).setName(“Total sales”);
//Number of cols/rows
Let nc = table.getColumns().length
Let nm = nc – 5;
Let nr = table.getRowCount();
//Set formula
Ts_1.setFormulasR1C1(“=sum(RC[-“ + nm + “];RC[-1])”);
//Autofit
Sheet.getRangeByIndexes(0,nc-1,nr,1).getFormat().autofitColumns();
}
PARA HACER EL CHART DE 3
Let data =
workbook.getWorksheet(“Diamonds”).getPivotTable(“DiamondsCutClarity”).getLayout().getRange();
(chartTypes[i], data);
Chart.setLeft(48.75);
Chart.setTop(28.5+i = 300);
Chart.setWidth(350);
Chart.setHeight(250);
Let data =
workbook.getWorksheet(“Diamonds”).getPivotTable(“DiamondsCutClarity”).getLayout().getRange();
timeSeries(workbook);
timeSeries2(workbook);
Chart.getSeries()[0].delete();
Chart.getAxes().getCategoryAxis().setNumberFormat(“yyyy”);
Chart.getAxes().getCategoryAxis().setTickLabelPosition(ExcelScript.ChartAxisTickLabelPosition.low);
Tline.setMovingAveragePeriod(4);
Tline.getFormat().getLine().setLineStyle(ExcelScript.ChartLineStyle.continuous);
Rate.getFormat().getLine().setLineStyle(ExcelScript.ChartLineStyle.dot);
Rate.getFormat().getLine().setWeight(1);
Chart.setLeft(55.5);
Chart.setTop(25.5);
Chart.setWidth(655.5);
Chart.setHeight(373.5);
Chart.getSeries()[0].setAxisGroup(ExcelScript.ChartAxisGroup.secondary);
Chart.getAxes().getCategoryAxis().setTickLabelPosition(ExcelScript.ChartAxisTickLabelPosition.low);
Chart.getLegend().setVisible(false);
Chart.setLeft(755.5);
Chart.setTop(25.5);
Chart.setWidth(655.5);
Chart.setHeight(373.5);