1. <i id='0XlMp'><tr id='0XlMp'><dt id='0XlMp'><q id='0XlMp'><span id='0XlMp'><b id='0XlMp'><form id='0XlMp'><ins id='0XlMp'></ins><ul id='0XlMp'></ul><sub id='0XlMp'></sub></form><legend id='0XlMp'></legend><bdo id='0XlMp'><pre id='0XlMp'><center id='0XlMp'></center></pre></bdo></b><th id='0XlMp'></th></span></q></dt></tr></i><div id='0XlMp'><tfoot id='0XlMp'></tfoot><dl id='0XlMp'><fieldset id='0XlMp'></fieldset></dl></div>

      <legend id='0XlMp'><style id='0XlMp'><dir id='0XlMp'><q id='0XlMp'></q></dir></style></legend>
    2. <small id='0XlMp'></small><noframes id='0XlMp'>

        <tfoot id='0XlMp'></tfoot>

        • <bdo id='0XlMp'></bdo><ul id='0XlMp'></ul>

        代码运行太慢

        时间:2023-10-02
      1. <legend id='YxBN0'><style id='YxBN0'><dir id='YxBN0'><q id='YxBN0'></q></dir></style></legend>

        <small id='YxBN0'></small><noframes id='YxBN0'>

          <tbody id='YxBN0'></tbody>

          <i id='YxBN0'><tr id='YxBN0'><dt id='YxBN0'><q id='YxBN0'><span id='YxBN0'><b id='YxBN0'><form id='YxBN0'><ins id='YxBN0'></ins><ul id='YxBN0'></ul><sub id='YxBN0'></sub></form><legend id='YxBN0'></legend><bdo id='YxBN0'><pre id='YxBN0'><center id='YxBN0'></center></pre></bdo></b><th id='YxBN0'></th></span></q></dt></tr></i><div id='YxBN0'><tfoot id='YxBN0'></tfoot><dl id='YxBN0'><fieldset id='YxBN0'></fieldset></dl></div>

            <tfoot id='YxBN0'></tfoot>
            • <bdo id='YxBN0'></bdo><ul id='YxBN0'></ul>

                1. 本文介绍了代码运行太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm trying to run a code that copies values from one spreadsheet and copies them to another, however the order is not the same(hard to make it an array). In some cases it also prints 'Unknown' and in some it also formats some cells. However it makes way to much time to finish. Is there a way to improve it?

                  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
                    }
                    }
                  

                  解决方案

                  The code should be optimised:

                  1. You do all calculations in a loop
                  2. You use getValue and setValue instead of faster functions getValues, setValues

                  Instead of this concentrate your loop to do a single call:

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

                  try to figure out how to find the first row outside the loop and then increment this value:

                  var rowStart = destinationSheet.getRange(destinationSheet.getLastRow()+1, 4,1,1).getRow();
                  
                  for (var row = rowStart; row <= lastRow, row++)
                  {
                    // some code...
                  }
                  

                  Use arrays and then copy the value from arrays into ranges:

                  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);
                  

                  And so on. See more info:

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

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

                  这篇关于代码运行太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:想要将 Rally 故事从一个项目复制到另一个项目 下一篇:如何将一个div的内容克隆到另一个div

                  相关文章

                2. <i id='V5HGL'><tr id='V5HGL'><dt id='V5HGL'><q id='V5HGL'><span id='V5HGL'><b id='V5HGL'><form id='V5HGL'><ins id='V5HGL'></ins><ul id='V5HGL'></ul><sub id='V5HGL'></sub></form><legend id='V5HGL'></legend><bdo id='V5HGL'><pre id='V5HGL'><center id='V5HGL'></center></pre></bdo></b><th id='V5HGL'></th></span></q></dt></tr></i><div id='V5HGL'><tfoot id='V5HGL'></tfoot><dl id='V5HGL'><fieldset id='V5HGL'></fieldset></dl></div>

                3. <small id='V5HGL'></small><noframes id='V5HGL'>

                    <bdo id='V5HGL'></bdo><ul id='V5HGL'></ul>

                  1. <legend id='V5HGL'><style id='V5HGL'><dir id='V5HGL'><q id='V5HGL'></q></dir></style></legend>

                    <tfoot id='V5HGL'></tfoot>