简体   繁体   English

用于计算连续单元格之间百分比的脚本 Google Sheet

[英]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:我使用下面的代码并得到了两种类型的问题:

  • my variable "percentageValue" return an array of [null,null,...]我的变量“percentageValue”返回一个数组 [null,null,...]
  • my final row generates the error message "Exception: The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 446."我的最后一行生成错误消息“异常:数据中的行数与范围内的行数不匹配。数据有 1 但范围有 446。”

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.

相关问题 Google Script - 在电子表格中自动创建新工作表后在预定义的单元格中设置公式 - Google Script - Setting formulas in pre-defined cells after automatically creating a new sheet in a spreadsheet 计算矩阵中每列的第一行和最后一行之间的百分比变化 - calculate percentage change between first and last row of each column in a matrix 填充Excel表格中同一列中两个相等单元格之间的所有空单元格(相等单元格的值相同) - Filling all the empty cells between two equal cells in same column in excel sheet (with the same value of the equal cells ) 有没有办法在多项选择中跨单元格迭代公式 - Google 工作表 - Is there a way to iterate a formula across cells in a multiple selection - Google sheet 为每一行谷歌工作表循环一个应用程序脚本 - Looping an apps script for every row of google sheet 如何为 Google 工作表制作循环脚本? - How to make a loop script for Google sheet? 使用 Google Script 循环浏览 Google Sheets 中某个范围内的单元格 - Looping through cells in a range in Google Sheets with Google Script 使用脚本应用从Google表格中的单元格提取日期 - Pulling Date From Cell in Google Sheet Using Script App 使用Google Apps脚本中的循环在工作表中搜索值 - Search for a value in a sheet using loop in Google Apps Script Google Script-如果连续 7 个单元格为空白,如何停止循环 - Google Script- how to stop a loop if 7 cells are blank in a row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM