简体   繁体   English

使用主表中的一行数据自动创建一个新表

[英]Automatically create a new sheet with data of a row in the mastersheet

Consider I have a mastersheet with names in column A and a link in colum B, starting in row 2.考虑我有一个母版表,其中 A 列中的名称和 B 列中的链接,从第 2 行开始。

The code below, for a reason I don't understand, creates only one new sheet with the name in row 2, and sets the value of the row 3 in the newly created sheet.下面的代码,出于我不明白的原因,只在第 2 行创建了一个名称为的新工作表,并在新创建的工作表中设置了第 3 行的值。

I'd like that to work as followed:我希望它按以下方式工作:

  • create a sheet with the name in col A创建一个名为 col A 的工作表
  • set values of col A and col B in the newly created sheet在新创建的工作表中设置 col A 和 col B 的值

Can someone help me figure out what's going on?有人可以帮我弄清楚发生了什么吗? I read the documentation about loops and getRange but I can't figure it out..我阅读了有关循环和 getRange 的文档,但我无法弄清楚..

Thank you very much非常感谢

function createNewSheets() {
  var dataSheet = SpreadsheetApp.getActiveSpreadsheet();
  var values = dataSheet.getSheetByName('mastersheet').getRange('A2:B').getValues();
  
  // Get sheet with current swimmer name:
  var sheet = dataSheet.getSheetByName(swimmerName);

  var col = values[i]; // Current row
  var swimmerName = col[0]; // swimmer name

  // Iterate through all rows of data from "mastersheet":
  for (var i = 1; i < values.length; i++) {

    // Check if sheet with current swimmer name exists. If it doesn't it creates it:
    if (!sheet) {
      sheet = dataSheet.insertSheet(swimmerName);
    }
    
    // Sets current row of data to the new sheet:
    sheet.getRange(1, 1, col.length, col[0].length).setValues(col)
  }
}

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the values from the columns "A" and "B" from "mastersheet" sheet.您想从“mastersheet”表中的“A”和“B”列中检索值。
  • You want to use the values of column "A" retrieved from "mastersheet" sheet as the sheet names.您想使用从“mastersheet”工作表中检索到的“A”列的值作为工作表名称。
  • When the sheet of the retrieved sheet name is not existing in the active Spreadsheet, you want to insert new sheet with the sheet name.当检索到的工作表名称的工作表在活动电子表格中不存在时,您希望插入具有工作表名称的新工作表。
  • When the new sheet is inserted, you want to copy the same row of the sheet name to the inserted sheet.插入新工作表时,您希望将工作表名称的同一行复制到插入的工作表中。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本实现此目的。

Modification points:修改点:

  • In your script,在你的脚本中,

    • At var sheet = dataSheet.getSheetByName(swimmerName) , swimmerName is not declaread.var sheet = dataSheet.getSheetByName(swimmerName)swimmerName未声明。
      • When swimmerName is not declared elsewhere, an error occurs at this line.当在其他地方没有声明swimmerName时,此行会发生错误。
    • At var col = values[i] , i is not declared.var col = values[i]处,未声明i
    • In the for loop,在 for 循环中,
      • the 1st index is 1 .第一个索引是1 From getRange('A2:B') , in this case, the values are retrieved from 3rd row.getRange('A2:B')开始,在这种情况下,值是从第 3 行检索的。
      • swimmerName and col are not changed. swimmerNamecol没有改变。
  • In order to achieve your goal, I would like to propose the following flow.为了实现您的目标,我想提出以下流程。

    1. Retrieve the current sheet names.检索当前工作表名称。
    2. Retrieve the values from "mastersheet" sheet.从“mastersheet”表中检索值。
    3. Using the retrieved sheet names and values, the new sheets are inserted and the values are put.使用检索到的工作表名称和值,插入新工作表并放置值。

When above flow is reflected to a script, it becomes as follows.当上述流程反映到脚本时,它变成如下。

Sample script:示例脚本:

function createNewSheets() {
  // 1. Retrieve the current sheet names.
  var dataSheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheetNames = dataSheet.getSheets().map(s => s.getSheetName());

  // 2. Retrieve the values from "mastersheet" sheet.
  var masterSheet = dataSheet.getSheetByName('mastersheet');
  var values = masterSheet.getRange('A2:B' + masterSheet.getLastRow()).getValues();

  // 3. Using the retrieved sheet names and values, the new sheets are inserted and the values are put.
  values.forEach(r => {
    if (!sheetNames.includes(r[0])) {
      dataSheet.insertSheet(r[0]).appendRow(r);
      sheetNames.push(r[0]);
    }
  });
}
  • In this sample script, sheetNames is growing in the loop and that is used for checking the duplicate sheet names.在此示例脚本中, sheetNames在循环中增长,用于检查重复的工作表名称。

References:参考:

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

相关问题 根据行数据值创建新的工作表 - Create new worksheets based on row data values 循环为行中的每个新数据创建新工作表-MS Excel - Looping to Create a New Worksheet for each new data in a Row - MS Excel 在CSV文件的新行上添加新数据 - Adding a new data on a new row in CSV file 创建新变量,将数据帧的所有变量除以每一行中的相同变量-R - Create new variables dividing all variables of a data frame by the same variable in each row - R 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 在多个数据中创建新变量 - Create new variables in multiple data R 函数使用前一行数据循环遍历新行 - R Function looping over new row using previous row data 使用 Google Apps 脚本添加/更新 Google 工作表行数据比较 HTML 表单中的值 - 尝试在工作表上查找匹配项并更新所选行数据 - Add/Update Google Sheet Row Data Comparing Values from HTML Form Using Google Apps Script - Trying to Find Match on Sheet & Update Selected Row Data 根据唯一的行名动态创建新的DataFrame - Dynamically create new DataFrames from unique row names 在 R 中创建新的 dataframe 列,以行值为条件而不进行迭代? - Create new dataframe column in R that conditions on row values without iterating?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM