Results 1 to 3 of 3

Office Script to Check if Table Header Exists

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    173

    Office Script to Check if Table Header Exists

    I'm working on a macro in OfficeScript.
    Among other things I need it to check if a column header exists in the given table and add a column if it doesn't exist. This part doesn't seem to work.

    Here is the relevant snippet
    function QAcolorTable(table: ExcelScript.Table){
      console.log("table: " + table.getName());
      
      //find or create column to for score formula
      let columnScore = table.getColumn("Score");
      console.log('null '+ (columnScore==null));
      console.log('type of ' + (typeof columnScore));
      console.log("undefined " + (columnScore==undefined));
      console.log("not " + !columnScore);
      console.log("void " + (columnScore == void 0));
      if (!columnScore) {
        console.log("adding 'Score'");
        table.addColumn(-1, null, "Score");
        columnScore = table.getColumn("Score");
      }
    }
    The office script documentation says that if getColumn should return 'undefined' if a column is not found, so I also tried number of tests for an undefined variable, but all return false.

    https://learn.microsoft.com/en-us/ja...lumn-member(1)

    So at this point I am thinking I may need to add a function to loop through all the headers, but that feels like overkill.
    Any suggestions?

    Here is my full code. I attached a sample workbook with appropriately named worksheet and table.
    function main(workbook: ExcelScript.Workbook) {
      let sheets = workbook.getWorksheets();
      sheets.forEach(sheet => {
        let sheetName = sheet.getName();
        if(sheetName.search("QA Checklist") > -1){
          QA_color_Sheet(sheet);
        }
      });
    }
    
    function QA_color_Sheet(sheet : ExcelScript.Worksheet){
      console.log("sheet: " + sheet.getName() );
      let tables = sheet.getTables();
      tables.forEach(table => {
        let tableName = table.getName();
        if(tableName.search("QAChecklist") > -1) {
          QAcolorTable(table);
        }
      });
    }
    
    function QAcolorTable(table: ExcelScript.Table){
      console.log("table: " + table.getName());
      
      const scoreFormula = "=IF( [@[Consultant Checked]]=\"N/A\",NA(),SWITCH([@[Get Colour]],38, 1, 36, 0.5, 35, 0, \"#C6EFCE\", 0, \"#FFEB9C\", 0.5, \"#FFC7CE\", 1, 1))";
      let headers = table.getHeaderRowRange();
    
      //get the column object to read colours from Exit if not found.
      let columnRead = table.getColumn("Initial Reviewer Comments");
      if (!columnRead) { columnRead = table.getColumn("Reviewer Comments"); }
      if (!columnRead) {
        console.log("column [Initial Reviewer Comments]||[Initial Reviewer Comments] not found in " + table.getName());
        return;
      }
      let rngRead = columnRead.getRangeBetweenHeaderAndTotal();
    
      //find or create colum to write RGB color to. 
      let columnWrite = table.getColumn("Get Colour");
      if (!columnWrite){
        console.log("Adding 'Get Colour'");
        table.addColumn(-1, null, "Get Colour");
        columnWrite = table.getColumn("Get Colour");
      }
      let rngWrite = columnWrite.getRangeBetweenHeaderAndTotal();
    
      //find or create column to for score formula
      let columnScore = table.getColumn("Score");
      console.log('null '+ (columnScore==null));
      console.log('type of ' + (typeof columnScore));
      console.log("undefined " + (columnScore==undefined));
      console.log("not " + !columnScore);
      console.log("void " + (columnScore == void 0));
      if (!columnScore) {
        console.log("adding 'Score'");
        table.addColumn(-1, null, "Score");
        columnScore = table.getColumn("Score");
      }
      console.log(scoreFormula)
      columnScore.getRangeBetweenHeaderAndTotal().getCell(0,0).setFormula(scoreFormula);
    
      let rows = table.getRowCount();
      for (let rw = 0; rw < rows; rw++) {
        let cellColorRead = rngRead.getCell(rw, 0);
        let cellWrite = rngWrite.getCell(rw, 0);
        cellWrite.setValue(cellColorRead.getFormat().getFill().getColor());
      }
    }
    Attached Files Attached Files
    Last edited by truk2; 08-17-2023 at 07:51 PM.
    If my solution helped, please consider adding Rep

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Office Script to Get Display Format (check if conditional formatting active)
    By BitcoinBadger in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-16-2022, 02:58 PM
  2. [SOLVED] SOLVED Help with Office Script to convert a Table to UPPER case
    By xxxrob in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2022, 05:50 PM
  3. Replies: 3
    Last Post: 08-14-2021, 05:25 PM
  4. Check if a Table Exists
    By mowens74 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-03-2020, 09:08 AM
  5. [SOLVED] Check if a value exists in a table
    By Kappany in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-08-2015, 02:42 AM
  6. Check if Column Header Exists?
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2014, 12:32 PM
  7. [SOLVED] Match a value in a table and print the header of that column if value exists
    By norms in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-25-2013, 06:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1