我在单元格中有以下数字作为文本:
/*
* 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);
});
});
}
现在我需要计算平均值,但是由于数据的长度不同,我不想创建额外的列。
有什么办法可以使用公式吗?
答案 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)