Need some help with my office script code

Sand Man 0 Reputation points
2023-02-05T02:16:46.2866667+00:00

Hi,

I need my code to run only in AC5:AC column but it runs in every cell, not sure what the problem is, where's my code:

function main(workbook: ExcelScript.Workbook) {
  // Get the specific sheet in the workbook.
  let sheet: ExcelScript.Worksheet = workbook.getWorksheet("DAILY");

  // Get the range of cells in column AC.
  let usedRange: ExcelScript.Range = sheet.getRange("AC:AC");

  // Get the used range of cells in column AC.
  let columnUsedRange: ExcelScript.Range = usedRange.getUsedRange(true);

  // Log the range's address to the console.
  console.log(columnUsedRange.getAddress());

  // Get the current date and time.
  let date: Date = new Date();
  let dateTime: string = date.toLocaleString();

  // Get the username
  let username: string = "user name"; // Use a default value for username
  console.log(`The current date and time is ${dateTime}`);
  console.log(`The current username is ${username}`);

  // Get the active cell
  let activeCell: ExcelScript.Range = workbook.getActiveCell();
  console.log(`The active cell's address is: ${activeCell.getAddress()}`);

  // Check if the active cell is in column AC
  if (activeCell.getColumn(29) === usedRange.getColumn(29)) {
    // Get the cell to the right of the active cell.
    let rightCell: ExcelScript.Range = activeCell.getOffsetRange(0, 1);
    let rightCellValue: string = rightCell.getValue().toString();

    // Set the value of the right cell to the concatenation of the date and time, the username, the cell's value, and the current value of the right cell after it.
    rightCell.setValue(`${dateTime} (${username}) > FINAL COMMENT: ${activeCell.getValue()}\n${rightCellValue}`);
  }
}

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,715 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
883 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,532 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,649 questions
{count} votes