简体   繁体   English

Google Apps 脚本中的循环和输出

[英]Looping and output in Google Apps Script

I am using 2 sheets, rawData and processedData .我使用2张, rawDataprocessedData

rawData looks like this: rawData看起来像这样:

Status Title Options Status Title Options
Live Title1 Option1, Option2, Option3, Option4 Live Title1 Option1, Option2, Option3, Option4
Live Title2 Option1, Option2, Option3, Option4, Option5 Live Title2 Option1, Option2, Option3, Option4, Option5
Live Title3 Option1, Option2, Option3 Live Title3 Option1, Option2, Option3 Title3 Option1, Option2, Option3 Title3

processedData should look like this: processedData应该是这样的:

Status Title Options Status Title Options
Live Title1 Option1 Live Title1 Option1
empty Title1 Option2 empty Title1 Option2
empty Title1 Option3 empty Title1 Option3
empty Title1 Option4 empty Title1 Option4
Live Title2 Option1 Live Title2 Option1
empty Title2 Option2 empty Title2 Option2
empty Title2 Option3 empty Title2 Option3
empty Title2 Option4 empty Title2 Option4
empty Title2 Option5 empty Title2 Option5
Live Title3 Option1 Live Title3 Option1
empty Title3 Option2 empty Title3 Option2
empty Title3 Option3 empty Title3 Option3

The empty is supposed to be empty, didn't know how to leave it blank to represent an empty cell. empty应该是空的,不知道如何将其留空来表示空单元格。

I tried using this code, but I can't get the desired output with the loops.我尝试使用此代码,但无法通过循环获得所需的输出。

function formatData() {

  // File
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Get rawData sheet
  var rawData = ss.getSheetByName('rawData');

  // Input data
  var data = rawData.getRange("A2:C").getValues(); // Gets titles and options in a single call to the sheet

  // Initialise an array that will hold the output
  var outputArray = [];

  // Name a variable to hold the data from each set of options
  var options;

  var status;

  // Start looping through the data
  for (var row = 0; row < data.length; row++) {

    status = data[row][0];

    outputArray.push([status]);

    // Split the options into an array: "Option1, Option2, Option3" ---> [Option1, Option2, Option3]
    options = data[row][1].split(", ");

    // Loop through the array of split options and place each of them in a new row
    for (var element = 0; element < options.length; element++) {

      outputArray.push([data[row][0], // Place the title in a new row
                        options[element]]); // Place one option in the 2nd column of the row

    } // Options loop ends here

  } // Data loop ends here

  // Get processedData sheet
  var processedData = ss.getSheetByName('processedData');

  // Get last row in processedData sheet
  var lastRow = processedData.getLastRow();

  // Post the outputArray to the sheet in a single call
  processedData.getRange(lastRow + 1, 1, outputArray.length, outputArray[0].length).setValues(outputArray);
}

I'm a total beginner in this language, sorry for asking a probably very basic question.我是这门语言的初学者,很抱歉问了一个可能非常基本的问题。

How about this modification?这个改装怎么样? Please think of this as just one of several modifications.请将此视为几种修改之一。

Pattern 1:模式一:

Modified script:修改后的脚本:

When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。 Please modify the for loop as follows.请按如下方式修改 for 循环。

for (var row = 0; row < data.length; row++) {
  status = data[row][0];
  var title = data[row][1]; // Added
//    outputArray.push([status]); // Removed
  options = data[row][2].split(", "); // Modified
  for (var element = 0; element < options.length; element++) {
    if (element == 0) { // Added and modified
      outputArray.push([status, title, options[element]]);
    } else {
      outputArray.push(["", title, options[element]]);
    }
  }
}

Pattetn 2:模式二:

As another pattern, how about the following modification?作为另一种模式,下面的修改怎么样? When you use this, please modify as follows.使用时请进行如下修改。

Modified script:修改后的脚本:

From: 从:
 // Initialise an array that will hold the output var outputArray = []; // Name a variable to hold the data from each set of options var options; var status; // Start looping through the data for (var row = 0; row < data.length; row++) { status = data[row][0]; outputArray.push([status]); // Split the options into an array: "Option1, Option2, Option3" ---> [Option1, Option2, Option3] options = data[row][1].split(", "); // Loop through the array of split options and place each of them in a new row for (var element = 0; element < options.length; element++) { outputArray.push([data[row][0], // Place the title in a new row options[element]]); // Place one option in the 2nd column of the row } // Options loop ends here } // Data loop ends here
To: 到:
 var outputArray = data.reduce(function(ar1, e) { var h1 = e.splice(0, 1)[0]; var h2 = e.splice(0, 1)[0]; var options = e[0].split(", "); return ar1.concat(options.reduce(function(ar2, f, j) { if (f) ar2.push(j == 0 ? [h1, h2, f] : ["", h2, f]); return ar2; }, [])); }, []);
  • The result is the same with the pattern 1.结果与模式 1 相同。

Note:笔记:

  • In your script, it seems that the header of "Status, Title, Options" is not copied.在您的脚本中,似乎没有复制“状态、标题、选项”的标题。 How about this?这个怎么样?

Reference:参考:

If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM