Coder Social home page Coder Social logo

Comments (3)

AlexJerabek avatar AlexJerabek commented on August 10, 2024

Hi @JohnHotham,

Thank you for the questions. I'm not quite sure I understand the scenario you're trying to accomplish, but I'll try my best.

First, you can't call a script from another script. You can have your script call multiple functions, like this:

 function main(workbook: ExcelScript.Workbook) {
    const selectedSheet = workbook.getActiveWorksheet();
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j <= i; j++) {
            highlightCell(selectedSheet.getRangeByIndexes(i, j, 1, 1));
        }
    }
}

function highlightCell(cell: ExcelScript.Range) {
    cell.getFormat().getFill().setColor("yellow");
}

You could also call multiple scripts from Power Automate. This scenario shows a flow that calls multiple scripts on the same workbook (granted, there are intermediate steps in the sample).

As for the filter question, if you want to apply a value-based filter, you'll applyValuesFilter on the Filter object of the TableColumn. In this example, the filter will only show rows when the value in the Description Rating is "Good" or "Fine".

function main(workbook: ExcelScript.Workbook) {
    const coverageWorksheet = workbook.getWorksheet("Coverage");
    const coverageTable = coverageWorksheet.getTable("CoverageTable");
    const descriptionColumn = coverageTable.getColumnByName("Description Rating");
    const descriptionSet = columnToSet(descriptionColumn);
    descriptionColumn.getFilter().applyValuesFilter(["Good", "Fine"]);
}

If you want to make a filter that keeps everything except a certain value, you'll need to add every other value to that filter array. In this example, we'll keep every row that doesn't have a description rating of "Poor". We'll use a helper function that converts all the values in a column to a set (effectively turning a 2D-array with duplicates into a 1D-array with unique values).

function main(workbook: ExcelScript.Workbook) {
    const coverageWorksheet = workbook.getWorksheet("Coverage");
    const coverageTable = coverageWorksheet.getTable("CoverageTable");
    const descriptionColumn = coverageTable.getColumnByName("Description Rating");
    const descriptionSet = columnToSet(descriptionColumn);
    descriptionColumn.getFilter().applyValuesFilter(descriptionSet.filter((value) => {
        return value !== "Poor";
    }));
}

/**
 * Convert a column into a set so it only contains unique values.
 */
function columnToSet(column: ExcelScript.TableColumn): string[] {
    const range = column.getRangeBetweenHeaderAndTotal().getValues() as string[][];
    const columnSet: string[] = [];
    range.forEach((value) => {
        if (!columnSet.includes(value[0])) {
            columnSet.push(value[0]);
        }
    });

    return columnSet;
}

I hope that helps. Please let me know if I missed the mark.

from office-scripts-docs.

JohnHotham avatar JohnHotham commented on August 10, 2024

Hi @AlexJerabek
Thanks for the assist, it is appreciated, I'll look at integrating them one at a time and see which works best. However, just looking at them, I'm reckoning the second one is a much closer fit than the others. Bear with me.

from office-scripts-docs.

AlexJerabek avatar AlexJerabek commented on August 10, 2024

Sounds good. Please let me know if that helps. If so, I'll work on creating a sample so others can benefit.

from office-scripts-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.