Nota
O acesso a esta página requer autorização. Podes tentar iniciar sessão ou mudar de diretório.
O acesso a esta página requer autorização. Podes tentar mudar de diretório.
Este projeto inclui dois scripts:
- Contar linhas em branco numa determinada folha: percorre o intervalo utilizado numa determinada folha de cálculo e devolve uma contagem de linhas em branco.
- Contar linhas em branco em todas as folhas: percorre o intervalo utilizado em todas as folhas de cálculo e devolve uma contagem de linhas em branco.
Observação
Para o script, uma linha em branco é qualquer linha onde não existem dados. A linha pode ter formatação.
Esta folha devolve a contagem de 4 linhas em branco
Esta folha devolve a contagem de 0 linhas em branco (todas as linhas têm alguns dados)
Observação
Execute estes exemplos diretamente a partir do Editor de Código de Scripts do Office. Para abrir o Editor de Código, aceda a Automatizar>a Criação de Novo Script>no Editor de Código. Substitua o código predefinido pelo código de exemplo que pretende executar e, em seguida, selecione Executar.
Código de exemplo: contar linhas em branco numa determinada folha
function main(workbook: ExcelScript.Workbook): number
{
// Get the worksheet named "Sheet1".
const sheet = workbook.getWorksheet('Sheet1');
// Get the entire data range.
const range = sheet.getUsedRange(true);
// If the used range is empty, end the script.
if (!range) {
console.log(`No data on this sheet.`);
return;
}
// Log the address of the used range.
console.log(`Used range for the worksheet: ${range.getAddress()}`);
// Look through the values in the range for blank rows.
const values = range.getValues();
let emptyRows = 0;
for (let row of values) {
let emptyRow = true;
// Look at every cell in the row for one with a value.
for (let cell of row) {
if (cell.toString().length > 0) {
emptyRow = false
}
}
// If no cell had a value, the row is empty.
if (emptyRow) {
emptyRows++;
}
}
// Log the number of empty rows.
console.log(`Total empty rows: ${emptyRows}`);
// Return the number of empty rows for use in a Power Automate flow.
return emptyRows;
}
Código de exemplo: Contar linhas em branco em todas as folhas
function main(workbook: ExcelScript.Workbook): number
{
// Loop through every worksheet in the workbook.
const sheets = workbook.getWorksheets();
let emptyRows = 0;
for (let sheet of sheets) {
// Get the entire data range.
const range = sheet.getUsedRange(true);
// If the used range is empty, skip to the next worksheet.
if (!range) {
console.log(`No data on this sheet.`);
continue;
}
// Log the address of the used range.
console.log(`Used range for the worksheet: ${range.getAddress()}`);
// Look through the values in the range for blank rows.
const values = range.getValues();
for (let row of values) {
let emptyRow = true;
// Look at every cell in the row for one with a value.
for (let cell of row) {
if (cell.toString().length > 0) {
emptyRow = false
}
}
// If no cell had a value, the row is empty.
if (emptyRow) {
emptyRows++;
}
}
}
// Log the number of empty rows.
console.log(`Total empty rows: ${emptyRows}`);
// Return the number of empty rows for use in a Power Automate flow.
return emptyRows;
}