Skip to content

Instantly share code, notes, and snippets.

@DominguesM
Created November 11, 2021 18:46
Show Gist options
  • Save DominguesM/10b78450446045a4ea9c638d4555a553 to your computer and use it in GitHub Desktop.
Save DominguesM/10b78450446045a4ea9c638d4555a553 to your computer and use it in GitHub Desktop.
Layout do suplemento: só um button com o texto "Formatar Planilha". Comportamento do suplemento: o script deve começar removendo qualquer formatação prévia nas células utilizadas na planilha. Em seguida, deve formatar uma planilha de notas de alunos previamente aberta no Excel (ou seja, use a planilha sem formatação gerada como resultado do exer…
name: lista 3 - 2
description: >-
Layout do suplemento: só um button com o texto "Formatar Planilha".
Comportamento do suplemento: o script deve começar removendo qualquer
formatação prévia nas células utilizadas na planilha. Em seguida, deve
formatar uma planilha de notas de alunos previamente aberta no Excel (ou seja,
use a planilha sem formatação gerada como resultado do exercício anterior),
aplicando uma formatação do tipo tabela do Excel com o "Estilo Médio 18".
Depois da formatação como tabela, ordene os nomes dos alunos alfabeticamente,
aplique formatação de número com uma casa decimal em todas as colunas com
valores numéricos, e pinte em vermelho o texto das médias finais inferiores a
6.
Tempo estimado para conclusão: 1 hora.
host: EXCEL
api_set: {}
script:
content: >
run.addEventListener("click", async () => {
await Excel.run(async function(context) {
let sheet = context.workbook.worksheets.getActiveWorksheet();
let table = sheet.tables.getItemOrNullObject("TabelaAluno");
table.load("isNullObject");
await context.sync();
if (table.isNullObject) {
alert("Atenção: Execute o suplemento do exercicio `Lista 3 - 1`, \n antes de executar este!");
}
// altera o estilo
table.style = 'TableStyleMedium18';
table.load('tableStyle');
// ordena o nome dos alunos
let sortFields = [
{
key: 0, // Coluna nome do aluno
ascending: true,
}
];
table.sort.apply(sortFields);
// formata numeros
const formats = [
["0.00"]
];
let body = table.getDataBodyRange();
var range = body.getColumnsAfter(-3);
range.numberFormat = formats;
// pinta as medias abaixo de 6 de vermelho
let column = body.getLastColumn();
column.load("rowCount");
await context.sync();
for (let i = 0; i < column.rowCount; i++) {
let row = column.getRow(i);
row.load("values");
await context.sync();
if (row.values[0][0] < 6) {
row.format.font.color = "Red";
};
};
// auto fill
table.getRange().format.autofitColumns();
table.getRange().format.autofitRows();
return context.sync();
}).catch(function(error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
});
// Força somente números nos inputs
//
https://www.geeksforgeeks.org/how-to-force-input-field-to-enter-numbers-only-using-javascript/
function onlyNumberKey(evt) {
// Only ASCII character in that range allowed
var ASCIICode = evt.which ? evt.which : evt.keyCode;
if (ASCIICode > 31 && (ASCIICode < 48 || ASCIICode > 57)) return false;
return true;
}
language: typescript
template:
content: |-
<button id="run" class="ms-Button">
<span class="ms-Button-label">Formatar</span>
</button>
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment