javascript - 代码运行速度太慢

标签 javascript optimization google-apps-script google-sheets copy

我正在尝试运行一段代码,将一个电子表格中的值复制到另一个电子表格中,但是顺序不同(很难使其成为数组)。在某些情况下,它还会打印“未知”,在某些情况下,它还会格式化某些单元格。然而,这需要很长时间才能完成。有办法改善吗?

function move() {

  var sss = SpreadsheetApp.openById('xx');
  var sourceSheet = sss.getSheetByName('CJ_Products');
  var destinationSheet = sss.getSheetByName('Product2');

  var lastRow = sourceSheet.getRange(sourceSheet.getLastRow(), 1,1,1).getRow()

  var i = 1

  while(i<=lastRow){
  var rowInt = destinationSheet.getRange(destinationSheet.getLastRow()+1, 4,1,1).getRow() //get row number
  destinationSheet.getRange('A' + rowInt).setFormula('=Month(D'+rowInt+')')
  destinationSheet.getRange('B' + rowInt).setFormula('=Weekday(D'+rowInt+')')
  destinationSheet.getRange('C' + rowInt).setFormula('=Day(D'+rowInt+')')
  destinationSheet.getRange('D' + rowInt).setValue(sourceSheet.getRange('A'+i).getValues()) //move from the source to destination
  destinationSheet.getRange('E' + rowInt+':F'+rowInt).setValue('Unknown') //set to Unknown
  destinationSheet.getRange('H' + rowInt+':J'+rowInt).setValue('Unknown')
  destinationSheet.getRange('J' + rowInt).setValue('CJ')
  destinationSheet.getRange('K' + rowInt).setValue(sourceSheet.getRange('B' +i).getValues())
  destinationSheet.getRange('L' + rowInt).setValue(sourceSheet.getRange('E' +i).getValues())
  destinationSheet.getRange('M' + rowInt).setValue(sourceSheet.getRange('F' +i).getValues())
  destinationSheet.getRange('N' + rowInt).setValue(sourceSheet.getRange('J' +i).getValues())
  destinationSheet.getRange('S' + rowInt).setValue(sourceSheet.getRange('G' +i).getValues())
  destinationSheet.getRange('T' + rowInt).setValue(sourceSheet.getRange('H' +i).getValues())
  destinationSheet.getRange('O' + rowInt).setFormula('=S'+rowInt+'*GOOGLEFINANCE("currency:EURUSD")')
  destinationSheet.getRange('P' + rowInt).setFormula('=T'+rowInt+'*GOOGLEFINANCE("currency:EURUSD")')
  destinationSheet.getRange('Q' + rowInt).setFormula('=P'+rowInt+'/T'+rowInt)
  destinationSheet.getRange('O' + rowInt+':Q'+rowInt).setNumberFormat('0.00$')

  i = i+1
  }
  }

最佳答案

应该优化代码:

  1. 您在循环中进行所有计算
  2. 您使用 getValuesetValue 而不是更快的函数 getValuessetValues

而不是集中循环来执行单个调用:

var rowInt = destinationSheet.getRange(destinationSheet.getLastRow()+1, 4,1,1).getRow()

尝试找出如何找到循环外的第一行,然后增加该值:

var rowStart = destinationSheet.getRange(destinationSheet.getLastRow()+1, 4,1,1).getRow();

for (var row = rowStart; row <= lastRow, row++)
{
  // some code...
}

使用数组,然后将值从数组复制到范围中:

var formulas = [];

for (var row = rowStart; row <= lastRow, row++)
{
  // some code...
  formulas.push(['=Month(D'+ row + ')']);
}
var rangeToPateFormulas = destinationSheet.getRange('A' + rowStart + ':A' + lastRow);
rangeToPateFormulas.setFormulas(formulas);

等等。查看更多信息:

https://developers.google.com/apps-script/reference/spreadsheet/range

https://developers.google.com/apps-script/guides/support/best-practices

关于javascript - 代码运行速度太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44512605/

相关文章:

c# - 是否可以从字符串列表中快速提取位于切片中标签内的特定值?

javascript - Google Apps 脚本 PropertiesService 访问冲突

google-apps-script - 在 Google 表格中,我如何编写默认缩放操作的脚本?

css - 为 Apps 脚本 UI 服务设置单独的 CSS 文件

javascript - 如何获取迭代对象的值

javascript - 如何在显示警报时尽量减少 if-else 语句

C++ std::string 初始化性能更好(汇编)

具有固定参数和的 R 优化

javascript - 在 for 循环中时,Jquery 赋值失败

javascript - 简单的 JavaScript 计数器(不使用 jQuery 或其他框架)