简体   繁体   English

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

[英]How to transpose and split in Google Apps Script?

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

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

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

processedData should look like this: processedData应如下所示:

Title Options Title Options
Title1 Option1 Title1 Option1
Title1 Option2 Title1 Option2
Title1 Option3 Title1 Option3
Title1 Option4 Title1 Option4
Title2 Option1 Title2 Option1
Title2 Option2 Title2 Option2
Title2 Option3 Title2 Option3
Title2 Option4 Title2 Option4
Title2 Option5 Title2 Option5
Title3 Option1 Title3 Option1
Title3 Option2 Title3 Option2
Title3 Option3 Title3 Option3

In Google Sheets, I would use the formula =TRANSPOSE(SPLIT(rawData!B2,",",TRUE,TRUE)) to get the options for the first title in the format I need. 在Google表格中,我将使用公式=TRANSPOSE(SPLIT(rawData!B2,",",TRUE,TRUE))获得所需格式的第一个标题的选项。

I'm trying to create a Google Apps Script that could take each row of rawData and convert it to the format shown in processedData . 我正在尝试创建一个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++;
  }
}

Executing this code will populate processedData like so: 执行此代码将以如下方式填充processedData

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

How can I get the script to split and transpose the options but write the same title for each option? 如何获得脚本来拆分和转置选项,但为每个选项写相同的标题?

Thank you in advance. 先感谢您。

PS: The code is likely not efficient and I assume it lacks any best practices, it's my first time using Google Apps Script so if anyone has any pointers I would appreciate it. PS:该代码可能效率不高,我认为它缺乏最佳实践,这是我第一次使用Google Apps脚本,因此,如果有人有任何指针,我将不胜感激。 I have been looking through Google's documentation but I need to sleep on it. 我一直在浏览Google的文档,但是我需要睡一会儿。

One way to solve this is to 解决此问题的一种方法是

  1. Split the options values 分割选项值
  2. and then build an output array with two columns and 13 (in this example) rows 然后构建一个具有两列和13行(在此示例中)的输出数组

Best practices suggest minimising calls to the sheet. 最佳做法建议尽量减少对工作表的呼叫。

So the following variation of your script does all of the above: 因此,脚本的以下变体可以完成上述所有操作:

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

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

相关问题 如何在谷歌应用程序脚本中用空格分隔符分割字符串 - How to split a string with space delimiter in google apps script Google Apps脚本:如何从一个电子表格中提取今天输入的数据并将其转换为另一电子表格? - Google Apps Script: How do I pull data entered today from one spreadsheet and transpose it to another? 复杂的转置超过了 Google Apps 脚本中的运行时间 - Complex transpose exceeding run time in Google Apps Script Pivot 或使用应用程序脚本在谷歌表中转置数据 - Pivot or Transpose data in a google sheet using apps script 拆分时的Google Apps脚本消息循环错误 - google apps script message loop error on split 使用Google Apps在脚本编辑器中拆分 - Split in script editor using google apps Google Apps 脚本 - 如何将具有多个选项卡的 Google 表格文件拆分为单独的文件 - Google Apps Script - How to Split a Google Sheets File With Multiple Tabs into Separate Files Google Spreadsheets ArrayFormula:如何拆分和转置单元格范围? - Google Spreadsheets ArrayFormula: How to split and transpose a cell-range? Google Apps 脚本:如何在二维数组上运行拆分 function 并保持列号相同 - Google Apps Script: How to run split function on 2D array and keep column numbers the same 如何在 Google Apps 脚本(在幻灯片中)中拆分文本(从表格中)? - How do I split text (from Sheets) in Google Apps Script (in Slides)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM