使用Powershell,如何将excel列中的值求和并按另一个变量的值排序?

时间:2019-02-07 15:21:00

标签: excel powershell sum

我正在尝试将数据从Excel电子表格导入Powershell脚本中,并在根据另一列的值对结果进行排序的同时,将一列的值相加。下面的示例可能比我用文字更好地说明了这一点。

最终目标是为每个特工在SUBMIT列中添加值,然后将该数据导出到单独的电子表格中,但是我无法弄清楚将这些值加在一起并与特工分组的第一步

const express = require("express");
const path = require("path-parser");
const cors = require("cors");
const bodyParser = require("body-parser");
const Sequelize = require("sequelize");
const epilogue = require("epilogue");
const OktaJwtVerifier = require("@okta/jwt-verifier");
const app = express();

const oktaJwtVerifier = new OktaJwtVerifier({
  clientId: "0oaacx81uD0ndjUyF356",
  issuer: "https://bigfootwebservice.okta.com/oauth2/ausacu2zhRbgylKNP356"
});

app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(async (req, res, next) => {
  try {
    if (!req.headers.authorization)
      throw new Error("Authorization header is required");

    const accessToken = req.headers.authorization.trim().split(" ")[1];
    await oktaJwtVerifier.verifyAccessToken(accessToken);
    next();
  } catch (error) {
    next(error.message);
  }
});

const database = new Sequelize({
  dialect: "sqlite",
  storage: "./test.sqlite"
});

const Post = database.define("posts", {
  title: Sequelize.STRING,
  body: Sequelize.TEXT
});

epilogue.initialize({ app, sequelize: database });

epilogue.resource({
  model: Post,
  endpoints: ["/posts", "/posts/:id"]
});

const PORT = process.env.SERVER_PORT || 3001;

app.listen = PORT;
console.log("Server is running");
console.log(process.env.NODE_ENV);

当我运行上面的代码块时,它成功地将“提交”列中的值相加,但未根据Agent对其进行排序。当我运行以下代码时,它会成功按代理对它们进行排序,但不会将它们加起来。必须有一种将它们组合在一起的方法,但是到目前为止,我一直没有成功。

foreach ($data in (Import-Excel "C:\prod\actstest.xlsx")) {
    $result += $data.Submit
}

Write-Host $result

这就是我得到的:

$testdata = Import-Excel "C:\prod\actstest.xlsx"

ForEach ($Agency in $testdata) {

    $AgentID = $Agency.AGENT 
    $Product = $Agency.EPROD
    $Submit = $Agency.SUBMIT

    [int]$SubNum = [convert]::ToInt32($Submit, 10)

        ForEach($AgentID in $Agency) {

            If ($Product -eq "HOP") {

                $Agency | Group-Object AGENT | %{
                New-Object psobject -Property @{
                Agent = $_.Name
                Sum = ($_.Group |  Measure-Object -property SUBMIT -Sum).Sum

                }

            }

        }

    }

}

这就是我想要得到的:

Sum Agent 
--- ----- 
  2 05007
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05008
  1 05018
  1 05018
  1 05018
  1 05018
  1 05018

2 个答案:

答案 0 :(得分:0)

由于我没有您的原始文件,并且无法真正真正地完全理解您要完成的工作,因此,我只能提供一种进行求和的方法。我创建了自己的代理数组以显示其工作原理:

$agents = @(05007,05007,05008,05007,05008,05009,05018,05008,05009,05017,05009)
$agentsSorted = $agents | sort
$uniqueAgents = $agentsSorted | get-unique

$data = foreach ($uniqueAgent in $uniqueAgents) {
$count = 0
$count = $agentssorted.where({$_ -eq $uniqueAgent}).count
$obj = [pscustomobject]@{Agent = $uniqueAgent; Sum = $count}
$obj
}

$data

答案 1 :(得分:0)

在不查看原始数据的情况下很难推断出所要寻找的东西。

这可能会做到:

model ConstantTest
  parameter Real h = 2;
  Real k;
initial equation
  k*k=h;
equation
  der(k) = 0;
end ConstantTest;