Office脚本:基于Excel中另一列中的值中的一个列中总和值的代码
我是 TypeScript 新手,今天的目标是根据另一列中的单元格值对 Excel 文件中某一列中的所有单元格值求和。
在我的 Excel 文件中,我将日历周存储在 U 列中,并将这些日历周的相应值存储在 R 列中。正如我在开头提到的,我的目标是总结每个日历周的所有值(列R 和相应的 U 列),并将每周的总和保存在 Excel 文件中的任意位置。
我的 Excel 文件在线存储在 OneDrive 上,我的数据也保存在那里。我打开 Excel 文件并进入“自动化选项卡”,它可以访问 Office 脚本,它相当于 Excel VBA(但使用 TypeScript 编程语言)。
我将屏幕与我想要做的事情连接起来我已经在下面做了什么。
- 以下是我的数据在 Excel 文件中的外观:
- 以下是我尝试使用 OfficeScript 解决问题的方法。 我试图用 do-while 循环来解决这个问题。
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.);
}
我正在利用这个SO帖子: VBA 代码根据 Excel 中另一列中的值对一列中的值求和 当尝试解决问题时
如果您对如何解决问题有任何建议,我们非常欢迎您的解决方案。非常感谢。
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.
- Here is how my data looks in my Excel file:
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试下面的代码。它首先要获取u和列列的范围和特定表的R列R。一旦拥有范围,它就会获得与这些范围关联的值。在您的日历列(U列)的值之后,我们将获得该列的唯一值。这些唯一的值存储在数组中。一旦在数组中具有唯一的值,就可以通过数组迭代。当您通过数组迭代时,您将当前数组元素与原始日历值进行比较。如果两个数组的元素都匹配,则将“和”列中的相应值添加到该行的变量。完成整个日历列完成迭代后,将唯一值和总计添加到地图对象中。此过程继续重复,直到唯一日历值数组的迭代完成为止。完成此操作后,函数将返回地图对象。
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.