简体   繁体   English

使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表

[英]Split data in google sheet to another sheet using google app script

I try to split data in google sheet by row and create new sheet and transfer the data to new sheet.我尝试按行拆分 google 工作表中的数据并创建新工作表并将数据传输到新工作表。 Here is my script sample:这是我的脚本示例:

var ss= getspreadsheetId();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange().getValues();
var lastRow = sheet.getLastRow();
var ui_user = SpreadsheetApp.getUi();
var result = ui_user.prompt("Enter split Number?"); 
var split_number=result.getResponseText();
var sheetName = ui_user.prompt("Enter Sheet name *Separate by space");
var sheetName_user =sheetName.getResponseText();
var sheetNamet_user_split=sheetName_user.split(" ");
data = sheet.getDataRange().getValues();
result = [];

//calculation to get row data split
split_value=Math.floor(lastRow/split_number);
//Logger.log(split_value);

 for(i=0;i<split_number;i++){

 for(j=0;j<split_value;j++){

 result.push(data[j]);


}
ss.insertSheet(sheetNamet_user_split[i]);

var sheetAdd = ss.getSheetByName(sheetNamet_user_split[i]);
sheetAdd.getRange(1,1,result.length,result[0].length).setValues(result);
result=[]

}

The problem in this script is i only able to display same data for every sheet that created.此脚本中的问题是我只能为创建的每个工作表显示相同的数据。 is there any way i can increment the iteration so it can iterate new data?有什么办法可以增加迭代,以便它可以迭代新数据?

Modification points:修改点:

  • In your script, the same values are used by for (j = 0; j < split_value; j++) { result.push(data[j]); }在您的脚本中, for (j = 0; j < split_value; j++) { result.push(data[j]); }使用相同的值for (j = 0; j < split_value; j++) { result.push(data[j]); } . for (j = 0; j < split_value; j++) { result.push(data[j]); } I think that this is the reason for your issue of The problem in this script is i only able to display same data for every sheet that created.我认为这就是您问题的原因The problem in this script is i only able to display same data for every sheet that created. . .
  • insertSheet method returns the inserted Sheet object. insertSheet方法返回插入的工作表 object。
  • When the number of inputted sheet names is more than the split values, the empty sheets are inserted.当输入的工作表名称的数量多于拆分值时,插入空工作表。

When these points are reflected in your script, how about the following modification?当这些点都体现在你的脚本中时,下面的修改怎么样?

From:从:

data = sheet.getDataRange().getValues();
result = [];

//calculation to get row data split
split_value=Math.floor(lastRow/split_number);
//Logger.log(split_value);

 for(i=0;i<split_number;i++){

 for(j=0;j<split_value;j++){

 result.push(data[j]);


}
ss.insertSheet(sheetNamet_user_split[i]);

var sheetAdd = ss.getSheetByName(sheetNamet_user_split[i]);
sheetAdd.getRange(1,1,result.length,result[0].length).setValues(result);
result=[]

}

To:到:

var data = sheet.getDataRange().getValues();
var split_value = Math.floor(lastRow / split_number);
sheetNamet_user_split.forEach(e => {
  var values = data.splice(0, split_number);
  if (values.length > 0) {
    var sheetAdd = ss.insertSheet(e.trim());
    sheetAdd.getRange(1, 1, values.length, values[0].length).setValues(values);
  }
});
  • In this modification, in order to use each split value, I used splice() .在此修改中,为了使用每个拆分值,我使用了splice()

References:参考:

暂无
暂无

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

相关问题 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script? 使用 Google App 脚本将数据从一张工作表复制到另一张工作表时执行缓慢 - Slow Execution for Copying Data from One Sheet to Another Using Google App Script Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 使用谷歌应用脚本将数据从谷歌表格中提取到一个谷歌文档文件中 - Pulling data into ONE google docs file from a google sheet using google app script Google App Script:格式化数据并粘贴到 Google 表格 - Google App Script: Format data and paste to google sheet 如何在不使用电子表格 ID 的情况下使用 Google 脚本从另一个 Google 表格导入数据 - How to import data from another Google Sheet using Google Script without using Spreadsheet ID 使用 Google App Script 修改 Google Sheet 中的单元格 - Modify a cell in Google Sheet using Google App Script Google脚本-根据另一个工作表上的复选框隐藏/取消隐藏Google工作表 - Google script - Hide/Unhide google sheet based on checkbox on another sheet 如何使用 Google Apps Script 在来自 Google 表格的多行中拆分 Google 幻灯片表中的数据(基于字符限制)? - How to split data in a Google Slides Table (based on character limit) in multiple rows coming in from a Google Sheet using Google Apps Script? 如何让 Snipe-IT API 使用 Google 应用脚本检索数据并将其填充到 Google 表格中? - How do I get Snipe-IT API to retrieve data using a Google App Script and populate it on a Google Sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM