Google Apps 脚本是 Javascript 的变体 - 因此,是的,它可以支持矩阵运算或您想要执行的任何其他数学运算。与 Javascript 一样,它本身无法执行此操作 - 您需要自己编写函数,或者找到适合的库。
特别是对于矩阵运算,这里有一个选项。何塞·德容mathjs https://stackoverflow.com/questions/tagged/mathjsNode.js 库在 Google Apps 脚本中按原样工作。您可以阅读它对矩阵的支持here http://mathjs.org/docs/datatypes/matrices.html.
复制最小化的math.js
原始来源(v. 6.6.4) https://unpkg.com/mathjs@6.6.4/dist/math.min.js,并将其粘贴到新脚本文件 https://developers.google.com/apps-script/managing_projects#creatingFiles在要将库添加到的脚本中。完成后,可以通过以下方式访问该库math
, e.g. math.someMethod()
尝试以下示例 - 注释显示了您在日志中可以期待的内容:
/**
* Demonstrate mathjs array & matrix operations.
*/
function matrix_demo() {
var array = [[2, 0],[-1, 3]]; // Array
var matrix = math.matrix([[7, 1],[-2, 3]]); // Matrix
// perform a calculation on an array and matrix
print( math.square(array) ); // Array, [[4, 0], [1, 9]]
print( math.square(matrix) ); // Matrix, [[49, 1], [4, 9]]
// perform calculations with mixed array and matrix input
print( math.add(array, matrix) ); // Matrix, [[9, 1], [-3, 6]]
print( math.multiply(array, matrix) ); // Matrix, [[14, 2], [-13, 8]]
// create a matrix. Type of output of function ones is determined by the
// configuration option `matrix`
print( math.ones(2, 3) ); // Matrix, [[1, 1, 1], [1, 1, 1]]
}
/**
* Helper function to output a value in the console. Value will be formatted.
* @param {*} value
*/
function print (value) {
var precision = 14;
Logger.log(math.format(value, precision));
}