以前复制的范围值被新复制的数据覆盖

时间:2017-10-31 17:00:10

标签: javascript google-apps-script

以前复制的范围值被新复制的数据覆盖 我正在开发的函数有4个。

第1部分:从位于“Ai:Pi”范围内的行“i”开始复制一行数据 - 其中i =行号,一次一个,到另一个位置 - 范围“A5:P5”(我在同一张纸上调用了输入数据暂存范围。工作精细

第2部分:然后将来自分段范围的数据拉入另一个电子表格进行处理。工作精细

第3部分:然后将来自其他电子表格的已处理数据复制回原始电子表格 - 范围“T5-AA5”(我称之为已处理数据的分段范围)。工作精细

第4部分:然后我想将VALUES仅从范围“T5:AA5”复制到与输入数据相同的行“i”,以便复制的范围是“Ti:AAi”。现在正在工作。

我已经测试了我在Excel中要做的事情,当我使用PASTE SPECIAL时效果非常好但是在Google Apps中问题是,当值被复制到右边的行“i”时,它们会被覆盖通过要复制的新数据范围。 我真的想解决第4部分。

我的代码是:

function getRowReturnValue(){

// "Form Responses 1" is the sheet with the Forms Data.
// The "Simple loop testing" sheet was set up to test the development of teh Apps scripts.

// Spreadsheet Sources
var ss = SpreadsheetApp.getActiveSpreadsheet();
var processor = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1fuGpv7YsyM5sCAMC25UkpfEURm06fJjyieezY3CjMl4/edit")// This is the PROCESSOR spreadsheet where 


// Data source sheets in the source spreadsheets
        // NOTE: IF YOU CHANGE THE SHEET NAME, THEN YOU NEED TO CHANGE TH SHEET NUMBER IN THE SQUARE BRACKETS BELOW FOR SOURCE and DESTINATION variables
        // [0} = sheet 1 = "Form Responses 1"; [1] = "Test Sheet"; [2] = "Simple loop testing"

var spr = ss.getSheetByName("Form Responses 1")
var processorOutputData = processor.getSheetByName("MASTER OUTPUT"); 

var source_sheet = ss.getSheets()[0];           // This is the location sheet of the FORMS INPUT data. In this case the "[0]" indicates the position of the very first sheet in the source spreadsheet
var target_sheet = ss.getSheets()[0];           // This is the location sheet of the PROCESSED data from the processor. In this case the "[0]" indiates the position of the very first sheet in the 
                                                // source spreadsheet


// Identify the number of rows of Forms Input data to be processed.  
var lastDataRow = spr.getLastRow();             // NOTE: This rule is identifies the last low number, ignoring blank rows in between. The disadvantage is that it looks at the WHOLE row and not just 
                                                // the cells of the row into which the Forms Input data will occupy
    Logger.log(lastDataRow);                    // CHECK

var NumbOfResponses = spr.getLastRow();         // CHECK
    Logger.log(NumbOfResponses);                // CHECK

// Clear the content of the staging row
var clearStagingRow = spr.getRange(5,1,1,16);   
    clearStagingRow.clearContent();


    for (var i = 10; i<NumbOfResponses+1;i++) {
       // Extract the input data VALUES, row by row, to be processed from the source sheet and put it into the INPUT STAGING area - which holds one row of data at a time.
       var values = source_sheet.getRange(i,1,1,16); 

           values.copyValuesToRange(target_sheet, 1, 16, 5, 5);               // Once the data is in the INOUT STAGING area, it is imported to the "MASTER OUTPUT" sheet in the processor spreadsheet.
                                                                              // This data is then processed and the resulting output data is posted in a range ready to be copied back into 
                                                                              // the "Forms responses 1" sheet

       // Identify the source range from the "MASTER OUTPUT" processor sheet. Identify the target range in the PROCESSED STAGING range in the "Forms responses 1" sheet
       var processedData = processorOutputData.getRange("d43:k43").getValues();            // Identify the range in the "MASTER OUTPUT" from where the data is to be sourced
       var target_range = target_sheet.getRange("T5:AA5").setValues(processedData);                    // Identify the target range in the PROCESSED STAGING area where the input is to be put


       // Copy PROCESSED OUTPUT data from OUTPUT STAGING range to the row alligned to the input data
       var retValues = source_sheet.getRange(5,20,1,8);
           retValues.copyValuesToRange(target_sheet, 20, 27, i, i);

       // Clear out the data previously copied into the OUTPUT STAGING data
          target_range.clearContent();

 } 

}

1 个答案:

答案 0 :(得分:0)

我已经审查并重新设计了模型,幸运的是简单性已经解决了这个问题。运行良好的代码是            var source_range = source_sheet.getRange(i,1,1,16); //确定数据来源的范围和位置            var source_values = source_range.getValues(); //在该位置获取值            var target_range = target_sheet.getRange(8,4,1,16); //确定数据在处理器中复制的位置和位置                target_range = target_range.setValues(source_values); //复制处理器中目标范围和位置的值

       var targetoutput_range = target_sheet.getRange(38,4,1,8); // identify range and location of where the processed data is
       var targetoutput_values = targetoutput_range.getValues(); // get the output values
       var destoutput_range = source_sheet.getRange(i,18,1,8);   // identify the range and location of where the output data will be copied
           destoutput_range = destoutput_range.setValues(targetoutput_values);  // copy the output values in the destination range and location

我在循环中运行上面的内容,并将输出值作为输入数据发布在同一行中。 我遇到的问题是,当inout行的数量变大时,处理数据所花费的时间就会增加。 我的答案是只评估新行,并保持“较旧”行不变。我的代码有效,但所用的时间不会缩短。 因此,如果有关于如何缩短时间的建议,请推荐。