Skip to content

Instantly share code, notes, and snippets.

@MiguelCriollo
Created November 6, 2023 04:06
Show Gist options
  • Save MiguelCriollo/ca5849b256c795807340f26035421332 to your computer and use it in GitHub Desktop.
Save MiguelCriollo/ca5849b256c795807340f26035421332 to your computer and use it in GitHub Desktop.
Performs a basic Excel API call using plain JavaScript & Promises.
name: Basic API call (JavaScript)
description: Performs a basic Excel API call using plain JavaScript & Promises.
host: EXCEL
api_set: {}
script:
content: >
$("#run").click(() => tryCatch(run));
$("#namesToChange").click(() => tryCatch(getNamesPosition));
$("#whereToPutLastNames").click(() => tryCatch(getWhereToPosition));
$("#whereToPutFirstNames").click(() => tryCatch(getWhereToPosition));
let trackedRangeNames;
let trackedRangeWhereToPut;
let charactersToDelete = ["Dr. ", "Dr.", "Dra. ", "Dra.", "Ing. ", "Ph.D.",
"PhD", "Ph.D.", "Ph. D.", "Ph.D", ","];
let expressionToDelete = new RegExp(charactersToDelete.join("|"), "g");
function getNamesPosition() {
return Excel.run(function(context) {
let sheet = context.workbook.worksheets.getActiveWorksheet();
let rangeNames = context.workbook.getSelectedRange().load("values")
return context.sync().then(function() {
console.log(rangeNames.values);
return context.sync();
}).catch(function(error) {
console.log(error);
});
});
}
function getWhereToPosition() {
return Excel.run(function(context) {
let sheet = context.workbook.worksheets.getActiveWorksheet();
let rangeWhereToPut = context.workbook.getSelectedRange().load("address");
return context.sync().then(function() {
trackedRangeWhereToPut = rangeWhereToPut;
context.trackedObjects.add(trackedRangeWhereToPut);
console.log(trackedRangeWhereToPut);
return context.sync();
})
.catch(function(error) {
console.log(error);
});
});
}
function run() {
return Excel.run(function(context) {
let sheet = context.workbook.worksheets.getActiveWorksheet();
//------------TRACKER EXTRACTION---------------------------------------------
context.trackedObjects.add(trackedRangeWhereToPut);
//------------Extractor de Posiciones---------------------------------------
const texto = trackedRangeWhereToPut.address;
const partes = texto.split('!');
const hoja = partes[0]; // "HojaX"
const datos = partes[1]; // "Q42"
const letras = datos.replace(/\d/g, ''); // Extraer letras
const numeros = datos.replace(/\D/g, ''); // Extraer números
//-----------------------------------------------------------------------------
let range = context.workbook.getSelectedRange().load("values");//Cargamos los valores seleciconados
// WHERE TO COPY
let where;
return context.sync().then(function () {
where = sheet.getRange(hoja + "!" + datos + ":" + letras + `${Number(numeros) + range.values.length -1}`).load("values");//Especificamos el rango a ser el mismo del que vamos a copiar
return context.sync();
}).then(function() {
let array = range.values;
for (let i = 0; i < array.length; i++) {
array[i][0] = range.values[i][0].replace(expressionToDelete, "");
}
//console.log(array);
where.values = array;
return context.sync();
}).then(function () {
})
.catch(function(error) {
console.log(error);
});
});
}
/** Default helper for invoking an action and handling errors. */
function tryCatch(callback) {
Promise.resolve()
.then(callback)
.catch(function(error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
});
}
language: typescript
template:
content: "<section class=\"ms-font-m\">\n\t<p class=\"ms-font-m\">Extractor de Nombres</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<button id=\"namesToChange\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Select Names</span>\n\t</button>\n\t<button id=\"whereToPutLastNames\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Selecto Where to Put Last Names</span>\n\t</button>\n\t<button id=\"whereToPutFirstNames\" class=\"ms-Button\">\n\t\t <span class=\"ms-Button-label\">Selecto Where to Put First Names</span>\n\t\t</button>\n\t<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Ejecutar Script</span>\n </button>\n</section>"
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
[email protected]
@types/[email protected]
@amirkhan7javi
Copy link

Hello, I merge and confirm the ownership. Thank you for all your cooperation. I am considering transferring to a valid wallet.
It should take more time to check the profit now

@MiguelCriollo
Copy link
Author

What are u taking about

@MiguelCriollo
Copy link
Author

I just did a commit, u gonna sue me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment