0

I am new to TypeScript and my goal today would be to sum up all the cell values in a column in an Excel file, based on the cell values in another column.

In my Excel file, I have the calendar weeks, stored in column U and the corresponding values for those calendar weeks in column R. As I mentioned at the beginning, my goal would be to sum up all the values for each calendar week (column R and corresponding column U), and save the overall sum for each week anywhere in an Excel file.

I have my Excel file on OneDrive online, with my data saved there. I open the Excel file and go into the "Automate Tab", which lets to have access to Office Scripts which is an equivalent of Excel VBA(but uses TypeScript programming language instead).

I attach the screens with what I was trying to do & what I have already done below.

  1. Here is how my data looks in my Excel file:

enter image description here

  1. Here is how I tried to solve the problem with the use of OfficeScripts. I was trying to solve the problem with a do-while loop.
function main(workbook: ExcelScript.Workbook)
{
  const sheet = workbook.getWorksheet('Sheet1'); 
  const range = sheet.getRange("A2:A6");
  const x: number = 1;
  let sum: number = 0;

  do 
  {
    if (sheet.getRange("A1").getOffsetRange(x,0) = '1')
    {
         sum = sum + sheet.getRange("A1").getOffsetRange(x,0)
    }
  } while(sheet.getRange("A").getColumn.);
}

I was making use of this SO post: VBA Code to sum values in one column based on the value in another column in excel when trying to solve the problem

If you have any suggestions as to how to solve the problem, your solutions are more than welcome. Thank you very much.

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
Mateusz Woś
  • 333
  • 2
  • 10
  • Screenshots of code aren’t as useful as the code itself. Makes it hard to copy and paste if it’s in an image. – Skin Apr 07 '22 at 09:30
  • 1
    Thanks for the feedback. I have now modified the post so that you can copy and paste the code if you have such a need. – Mateusz Woś Apr 07 '22 at 09:56

1 Answers1

1

You can give the code below a try. It starts by getting the ranges for column U and column R for a specific sheet. Once it has the ranges, it gets the values associated with those ranges. After we have the values for your calendar column (column U), we get the unique values for that column. Those unique values are stored in an array. Once you have the unique values in an array, you iterate through the array. As you iterate through the array, you compare the current array element to the original calendar values. If the elements of both arrays match, you add the corresponding value in the sum column to a variable for that row. Once you've finished iterating through the full calendar column, the unique values and total are added to a Map object. This process continues repeats until the iteration of the unique calendar values array is completed. After that's done, the map object is returned by the function.

function main(workbook: ExcelScript.Workbook) {
    let ws: ExcelScript.Worksheet = workbook.getWorksheet("Sheet1")
    let criteriaStart: string = "U2"
    let sumStart: string = "R2"
    let map: Map<string, number> = getColumnTotals(ws, criteriaStart, sumStart);

    //After you have the map you can access specific elements like this

    console.log(map.get('2022 CW12'));

    //Or you can access all the elements in the map like this

    map.forEach(e => console.log(e))
}

function getColumnTotals(worksheet: ExcelScript.Worksheet, criteriaStartAddress: string, sumStartAddress: string): Map<string, number> {
    let criteriaRange: ExcelScript.Range = worksheet.getRange(criteriaStartAddress).getExtendedRange(ExcelScript.KeyboardDirection.down);
    let criteriaVals: string[][] = criteriaRange.getValues() as string[][];
    let sumRange: ExcelScript.Range = worksheet.getRange(sumStartAddress).getExtendedRange(ExcelScript.KeyboardDirection.down);
    let sumVals: number[][] = sumRange.getValues() as number[][];
    let map: Map<string, number> = new Map<string, number>();
    let tempArr: string[] = criteriaVals.map(e => e[0]);
    let uniqueCalendarVals: string[] = Array.from(new Set(tempArr));

    uniqueCalendarVals.forEach(uniqueCalVal => {
        let tempTotal: number = 0;
        criteriaVals.forEach((criVal, index) => {
            if (criVal[0] === uniqueCalVal) {
                tempTotal += sumVals[index][0];
            }
        })
        map.set(uniqueCalVal, tempTotal);
    });
    return map;
}
Brian Gonzalez
  • 1,178
  • 1
  • 3
  • 15
  • Hi many thanks for your answer and sorry for my late reply. Could you please tell if the function at the bottom of your answer needs to be included within the code you have posted at the beginning? Does it need to be taken into account as OneScript or are there two separate Scripts that I would need to create in order for your code to run properly? – Mateusz Woś Apr 25 '22 at 11:00
  • There is no need for you to reply - I figured out what was wrong. I noticed that at the beginning of your code all I needed to was to add a few lines of code to the 'main' function. I am therefore marking this question as solved and your answer as the correct one, – Mateusz Woś Apr 25 '22 at 11:17
  • Thank you for your feedback. I will update my example to be more clear. – Brian Gonzalez Apr 25 '22 at 14:49