[英]Script to calculate percentage between consecutive cells Google Sheet
I have a list of daily close prices for bitcoin in a column B and am trying to write a script that would put the %change between row i and rowi-1 in the adjacent column C (column A is the date).我在 B 列中有比特币的每日收盘价列表,我正在尝试编写一个脚本,将第 i 行和第 1 行之间的百分比变化放在相邻的 C 列中(A 列是日期)。 The calculation needs to start from the last row and go back up to row number 2.计算需要从最后一行开始,go 回溯到第 2 行。
I used the code below and get 2 types of issues:我使用下面的代码并得到了两种类型的问题:
For the logic I tried to work with 2 variable I could divide for the percentage calculation: rangePriceValues (prices at day 2) and rangePricelastValues (values at day 1).对于我尝试使用 2 个变量的逻辑,我可以划分百分比计算:rangePriceValues(第 2 天的价格)和 rangePricelastValues(第 1 天的值)。
I tried to find similar code online and couldn't find anything close (but am sure there must be one somewhere) I am new to Java and would really appreciate if someone could put me on the right track.我试图在网上找到类似的代码,但找不到任何接近的东西(但我确信某处肯定有一个)我是 Java 的新手,如果有人能让我走上正确的轨道,我将不胜感激。
Thanks!!谢谢!!
function PopulateC()
{
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test__Price");
var rangePrice = sheet1.getRange(2,2,sheet1.getLastRow(),1); // full list of prices
var rangePriceValues = rangePrice.getValues(); // value of these prices
var secondLastRow = rangePrice.getNumRows()-2; //total number of rows minus one value
var rangePricelast = sheet1.getRange(2,2,secondLastRow.valueOf() ,1); // full list of prices minus one
var rangePricelastValues = rangePricelast.getValues(); //value of the prices at day+2
var percentage = [];
var percentageValue = [];
var rangePercentage = sheet1.getRange(2,3,rangePrice.getNumRows(),1);
for (i= rangePrice.getNumRows(); 2; i--)
{
percentage [i-1] = (rangePriceValues[i]/= rangePricelastValues[i]-1)* 100;
percentageValue= percentage.valueOf();
Logger.log(percentageValue) // this is where I see my array with [null, null,....]
rangePercentage.setValues([percentageValue]);
}
}
`
Change this: var rangePrice = sheet1.getRange(2,2,sheet1.getLastRow(),1);
改变这个: var rangePrice = sheet1.getRange(2,2,sheet1.getLastRow(),1);
to This: var rangePrice = sheet1.getRange(2,2,sheet1.getLastRow()-1,1);
为此: var rangePrice = sheet1.getRange(2,2,sheet1.getLastRow()-1,1);
I found a way to solve the problem by writing a simpler code.我找到了一种通过编写更简单的代码来解决问题的方法。 It works but the calculation is very slow.它可以工作,但计算速度很慢。 If anyone has a view on how to optimise the code I would be super grateful, as I am still learning.如果有人对如何优化代码有意见,我将非常感激,因为我还在学习。 Thanks Cooper for the suggestions!感谢库珀的建议!
function SimplePerc() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test_Bitcoin_Price");
var rangePrice = sheet1.getRange(2,2,445,1);
var priceValue = rangePrice.getValues();
for (var i=sheet1.getLastRow() ; 2; i--)
{
var numerator = sheet1.getRange(i,2).getValue();
var denominator = sheet1.getRange(i+1,2).getValue();
var percentagecalc = (numerator /= denominator);
var percentageclean = (percentagecalc -1)*100;
sheet1.getRange(i,3).setValue(percentageclean);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.