谷歌电子表格数组公式与粘贴最佳实践

jas*_*son 6 google-sheets

谷歌电子表格中数组公式的最佳实践问题

假设我在 A 列有一列值。在 B 列中,我乘以 10。我可以对整个范围使用数组公式,或者在 B 列中创建第一个单独的公式并粘贴下来。

我的问题是哪个更好用?数组计算速度更快吗?从速度的角度来看有什么好处吗?我有很大的电子表格,我想知道这是否会有所作为。

Ada*_*amL 6

我真的无法为这个答案提供任何参考,它是基于轶事证据和与其他 Google Sheets 用户的讨论。

对于相对简单的计算,例如您问题中的计算,我相信数组解决方案对于非常大的数据集具有更好的性能。它还减少了超出表格中公式限制的可能性(40,000,但由数组公式填充的 CONTINUE 函数不会影响此计数)。

然而,对于非常复杂的计算,这两个选项确实都有能力让电子表格陷入停顿。臭名昭著的例子是数组公式,我们需要诉诸字符串操作(例如,将数组连接在一起,然后再次将它们分开)。在这种情况下,我个人会采用 C 计划,编写一个 Google Apps 脚本自定义函数,该函数将一个数组(arrays)作为参数(arguments),使用纯 Javascript 来操作数据,并输出一个数组。


xyz*_*333 6

您可以使用两个脚本对此进行测试

将 30,000 行相乘,其中每个单元格为 10 x 10

自动扩展需要约 0.275 秒 CopyDown 约需 0.678 秒

https://docs.google.com/spreadsheets/d/1djHUUp_kTS02gYnKf5Y3AvpHCIt_69x_X02eesOSywzw/edit?usp=sharing

 function AutoExpand() {
 var ss    =SpreadsheetApp.getActive();
 var sheet =ss.getSheetByName('AAA');
 var LC    = sheet.getLastColumn();
 var LR    = sheet.getLastRow();

 var start = new Date();
 //Auto-expanding Formulas to be added
 //Two dim array with 1 row
 var formulas = [["=ArrayFormula(A:A*10)"]];

 //Add auto-expanding formulas to Cell(s)
 var cell = sheet.getRange(1,LC+1,1,formulas[0].length);
 cell.setFormulas(formulas);
 SpreadsheetApp.flush();

 //Get range and post back Display Values
 //var r = sheet.getRange(1,LC+1,LR,formulas[0].length);
 //var v = r.getDisplayValues();
 //r.setValues(v);

 var end = new Date();
 var executiontime = end - start;
 Logger.log(executiontime); 
 }
Run Code Online (Sandbox Code Playgroud)

向下复制

function CoppyDown() {
var ss    =SpreadsheetApp.getActive();
var sheet =ss.getSheetByName('AAA');
var LC    = sheet.getLastColumn();
var LR    = sheet.getLastRow();

var start = new Date();
//NON Auto-expanding Formula(s) to be added
//Two dim array with 1 row
var formulas = [["=A:A*10"]];

//Add NON auto-expanding formula(s) to Cell(s)
var cell = sheet.getRange(1,LC+1,1,formulas[0].length);
cell.setFormulas(formulas);
SpreadsheetApp.flush();

//Get range FULL Range of Cells W/Formulas
var r = sheet.getRange(1,LC+1,LR,formulas[0].length);

//Add formulas to Row1 Cell(s)
var cells = sheet.getRange(1,LC+1,1,formulas[0].length);
cells.setFormulas(formulas);

//Copy formulas down
cells.copyTo(r);
SpreadsheetApp.flush();

//Get the Display Values of the Range W/Formulas
//var v = r.getDisplayValues();

//Clear the Formulas Before the Postback
//This Super Speeds up the Postback
//r.clear();

//Postback Formulas as Values
//r.setValues(v);

var end = new Date();
var executiontime = end - start;
Logger.log(executiontime); 
}
Run Code Online (Sandbox Code Playgroud)