簡體   English   中英

Google Apps 腳本中的循環和輸出

[英]Looping and output in Google Apps Script

我使用2張, rawDataprocessedData

rawData看起來像這樣:

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

processedData應該是這樣的:

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

empty應該是空的,不知道如何將其留空來表示空單元格。

我嘗試使用此代碼,但無法通過循環獲得所需的輸出。

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

我是這門語言的初學者,很抱歉問了一個可能非常基本的問題。

這個改裝怎么樣? 請將此視為幾種修改之一。

模式一:

修改后的腳本:

當你的腳本被修改時,它變成如下。 請按如下方式修改 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]]);
    }
  }
}

模式二:

作為另一種模式,下面的修改怎么樣? 使用時請進行如下修改。

修改后的腳本:

從:
 // 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
到:
 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; }, [])); }, []);
  • 結果與模式 1 相同。

筆記:

  • 在您的腳本中,似乎沒有復制“狀態、標題、選項”的標題。 這個怎么樣?

參考:

如果我誤解了您的問題並且這不是您想要的結果,我深表歉意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM