如何在 Google Apps 脚本中转置和拆分?

How to transpose and split in Google Apps Script?

我正在使用 2 张纸,rawDataprocessedData

rawData 看起来像这样:

Title Options
Title1Option1, Option2, Option3, Option4
Title2Option1, Option2, Option3, Option4, Option5
Title3 Option1, Option2, Option3

processedData 应该是这样的:

Title Options
Title1 Option1
Title1Option2
Title1Option3
Title1Option4
Title2Option1
Title2Option2
Title2Option3
Title2Option4
Title2Option5
Title3Option1
Title3Option2
Title3 Option3

在 Google 表格中,我会使用公式 =TRANSPOSE(SPLIT(rawData!B2,",",TRUE,TRUE)) 以我需要的格式获取第一个标题的选项。

我正在尝试创建一个 Google Apps 脚本,它可以获取 rawData 的每一行并将其转换为 processedData 中显示的格式。

function formatData() {
  // File
  var ss = SpreadsheetApp.getActiveSpreadsheet();

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

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

  // Get range that contains titles, get its values
  var titlesRange = rawData.getRange('A2:A');
  var titlesRangeValues = titlesRange.getValues();

  // Get range that contains options, get its values
  var optionsRange = rawData.getRange('B2:B');
  var optionsRangeValues = optionsRange.getValues();

  // Get number of rows in rawData sheet
  var titlesCount = GSheetsUtils.getRowsData(rawData);

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

  // Copy each title to processedData and format the options
  for (var i = 0; i<titlesCount.length; i++) {
    processedData.getRange(lastRow + 1,1).setValue(titlesRangeValues[i][0]);
    processedData.getRange(lastRow + 1,2).setValue(optionsRangeValues[i][0]);
    lastRow++;
  }
}

执行此代码将像这样填充 processedData

Title Options
Title1Option1, Option2, Option3, Option4
Title2Option1, Option2, Option3, Option4, Option5
Title3 Option1, Option2, Option3

如何让脚本拆分和转置选项,但为每个选项写相同的标题?

提前致谢。

PS:代码可能效率不高,我认为它缺乏任何最佳实践,这是我第一次使用 Google Apps 脚本,所以如果有人有任何指示,我将不胜感激。我一直在查看 Google 的文档,但我需要休息一下。

解决这个问题的一种方法是

  1. 拆分选项值
  2. 然后构建一个包含两列和 13(在此示例中)行的输出数组

Best practices 建议尽量减少对 sheet.

的调用

因此,您的脚本的以下变体可以完成上述所有操作:

function formatData() {

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

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

  // Input data
  var data = rawData.getRange("A2:B").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;

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

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