Libreoffice Calc:对文本中的数字求和

时间:2019-02-13 17:26:23

标签: libreoffice libreoffice-calc

我在单元格中有以下数字作为文本:

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var userID = document.getElementById("userID");
const url1 = "https://jsonplaceholder.typicode.com/users/";
const url2 = "https://jsonplaceholder.typicode.com/users";
//JSON.stringify(data)
function getUser() {
  fetch(url1 + userID.value)
    .then(res => res.json())
    .then(data => {
      var all = "<p>" + "Name: " + data.name + "</p>";
      all += "<p>" + "Phone: " + data.phone + "</p>";

      document.getElementById("singleUser").innerHTML = all;
    });
}

//***************Code that needs look on is right here ********************

function getAllUsers() {
  fetch(url2)
    .then(res => res.json())
    .then(data => {
      for (var i = 0; i < data.length; i++) {
        console.log(data[i]);
        var all = "<p>" + "Name: " + data[i].name + "</p>";
        all += "<p>" + "Phone: " + data[i].phone + "</p>";
        document.getElementById("allUsers").innerHTML = all;
      }

      //for (var key in data) {
      //    if(data.hasOwnProperty(key)) {
      //        document.getElementById("allUsers").innerHTML = data[key].name;
      //    }
      //}

      data.forEach(function(key) {
        var users = "<p>" + "Name: " + key.name + "</p>";
        users += "<p>" + "Phone: " + key.phone + "</p>";
        document.getElementById("allUsers").innerHTML = users;
        console.log(key.name);
      });
    });
}

现在我需要计算平均值,但是由于数据的长度不同,我不想创建额外的列。

有什么办法可以使用公式吗?

2 个答案:

答案 0 :(得分:0)

换句话说:您想将字符串值除以“-”字符并计算其元素的平均值? AFAIK解决此问题的唯一方法是使用小宏(AKA用户定义的函数),因为LO Calc在电子表格级别上未提供拆分/代币化功能。

一种快速而肮脏的解决方案可能如下所示:

Function split_average(a)
    Dim theArray(UBound(Split(a, "-"))) As Integer
    theArray = Split(a, "-")
    Dim SumVal As Integer
    For i = 0 To UBound(theArray)
        SumVal = SumVal + theArray(i)
    Next i
    split_average = SumVal / (UBound(theArray) + 1)
End Function

当然,没有类型检查等,因此请您自担风险。要使用它,只需将其复制到StarBasic Standard模块中,保存,然后使用=split_average(A1)在电子表格中调用它。有关一般用户定义的功能,请参见the LO Calc docs

答案 1 :(得分:0)

我知道这是旧线程,但让我加两分钱。使用FILTERXML和一些XPATH魔术=)


=FILTERXML("<t><s>"&REGEX(A1;"-";"</s><s>";"g")&"</s></t>";"sum(//s) div count(//s)")

enter image description here


我们甚至可以对数字节点进行检查:

=FILTERXML("<t><s>"&REGEX(A1;"-";"</s><s>";"g")&"</s></t>";"sum(//s[.*0=0]) div count(//s[.*0=0])")

enter image description here


对此,我发现很遗憾,Excel不允许在表达式本身内部进行直接操作。 LibreOffice令人惊讶!

相关问题