简体   繁体   English

使用 Google 脚本移动新添加的列

[英]Moving Newly Added Columns using Google Scripts

I have a script that adds in new data daily from our production line.我有一个脚本,每天从我们的生产线上添加新数据。 However since it just appends the data the format is not the way we need it to be for presentation.但是,由于它只是附加数据,因此格式不是我们需要的呈现方式。

The format we need is: Order # - Date - Line - Priority我们需要的格式是:订单号 - 日期 - 行 - 优先级

The format we import is: Date - Line - Priority - Order #我们导入的格式是:日期 - 行 - 优先级 - 订单#

This is the script that we use to add data in daily:这是我们每天用来添加数据的脚本:

  function CopySchedule() {

  var Production = SpreadsheetApp.openById("ProductionID"); //Production Schedule
  var ScheduleSheet = Production.getSheets()[0]; //Production Schedule Sheet 1
  var startRow = 2;
  var startCol = 1;
  var numRows = 1000;
  var numCol = 25;
  var ScheduleData = ScheduleSheet.getRange(startRow, startCol, numRows, numCol); //Get info from Production Schedule
  var Schedule = ScheduleData.getValues(); // Get Values
  var MySchedule = SpreadsheetApp.getActiveSpreadsheet(); //Current Spreadsheet
  var MySheet = MySchedule.getSheets()[5]; //Current Sheet on Spreadsheet
  var now = new Date(); //Today's date
  var today = Utilities.formatDate(now, "EST", "M-dd-yy"); //Reformat today date
    
  for (var x = 0; x < 1000; x++) 
  {

     if (Schedule[x][0] == today) //if date is today
       {
          MySheet.appendRow(Schedule[x]); //add row from schedule to bottom of current sheet
        }
  }
  

}

This is how we want the Data to be presented这就是我们希望数据呈现的方式

This is how it is added into the sheet这就是它添加到工作表中的方式

Macros wouldn't be sufficient since the amount of orders we do daily isn't a fixed # unless there's a way to have a macro only select the new daily data.宏是不够的,因为我们每天执行的订单数量不是固定的 # 除非有办法让宏仅 select 新的每日数据。

Problem statement:问题陈述:

You need to store the values in the desired order.您需要以所需的顺序存储这些值。 Based on the screenshots provided, the right order should be:根据提供的屏幕截图,正确的顺序应该是:

[Schedule[x][3],Schedule[x][0],Schedule[x][1],Schedule[x][2]]

Solutions:解决方案:

There are two ways you can acomplish that.有两种方法可以实现。 One way is to use appendRow() which is not recommended especially when the dataset becomes large and the second way is to use setValues() which is way more efficient.一种方法是使用不推荐的appendRow() ,尤其是当数据集变大时,第二种方法是使用更有效的setValues()

1st Solution with appendRow() :使用appendRow()的第一个解决方案:

function CopySchedule() {

  var Production = SpreadsheetApp.openById("ProductionID"); //Production Schedule
  var ScheduleSheet = Production.getSheets()[0]; //Production Schedule Sheet 1
  var startRow = 2;
  var startCol = 1;
  var numRows = 1000;
  var numCol = 25;
  var ScheduleData = ScheduleSheet.getRange(startRow, startCol, numRows, numCol); //Get info from Production Schedule
  var Schedule = ScheduleData.getValues(); // Get Values
  var MySchedule = SpreadsheetApp.getActiveSpreadsheet(); //Current Spreadsheet
  var MySheet = MySchedule.getSheets()[5]; //Current Sheet on Spreadsheet
  var now = new Date(); //Today's date
  var today = Utilities.formatDate(now, "EST", "M-dd-yy"); //Reformat today date
    
  for (var x = 0; x < 1000; x++) 
  {

     if (Schedule[x][0] == today) //if date is today
       {
          MySheet.appendRow([Schedule[x][3],Schedule[x][0],Schedule[x][1],Schedule[x][2]]); 
        }
  }
  

}

2nd Solution with setValues() - Recommended:使用setValues()的第二个解决方案 - 推荐:

 function CopySchedule() {

  var Production = SpreadsheetApp.openById("ProductionID"); //Production Schedule
  var ScheduleSheet = Production.getSheets()[0]; //Production Schedule Sheet 1
  var startRow = 2;
  var startCol = 1;
  var numRows = 1000;
  var numCol = 25;
  var ScheduleData = ScheduleSheet.getRange(startRow, startCol, numRows, numCol); //Get info from Production Schedule
  var Schedule = ScheduleData.getValues(); // Get Values
  var MySchedule = SpreadsheetApp.getActiveSpreadsheet(); //Current Spreadsheet
  var MySheet = MySchedule.getSheets()[5]; //Current Sheet on Spreadsheet
  var now = new Date(); //Today's date
  var today = Utilities.formatDate(now, "EST", "M-dd-yy"); //Reformat today date
    
  data = [];
  for (var x = 0; x < 1000; x++) 
  {

     if (Schedule[x][0] == today) //if date is today
       {
          data.push([Schedule[x][3],Schedule[x][0],Schedule[x][1],Schedule[x][2]]);
        }
  }
  
MySheet.getRange(MySheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM