gpt4 book ai didi

javascript - 代码运行太慢

转载 作者:行者123 更新时间:2023-11-29 17:50:18 25 4
gpt4 key购买 nike

我正在尝试运行一个代码,从一个电子表格复制值并将它们复制到另一个电子表格,但是顺序不一样(很难使其成为一个数组)。在某些情况下,它还会打印“Unknown”,在某些情况下它还会格式化一些单元格。但是,它需要很多时间才能完成。有没有办法改善它?

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com