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

Moving Newly Added Columns using Google Scripts

我有一个脚本,每天从我们的生产线添加新数据。但是,由于它只是附加数据,因此格式不是我们需要的显示方式。

我们需要的格式是: 订单号 - 日期 - 行 - 优先级

我们导入的格式是: 日期 - 行 - 优先级 - 订单 #

这是我们每天用来添加数据的脚本:

  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

宏是不够的,因为我们每天的订单量不是固定的 # 除非有办法让宏只 select 新的每日数据。

问题陈述:

您需要按所需顺序存储值。根据提供的屏幕截图,正确的顺序应该是:

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

解决方案:

有两种方法可以实现。一种方法是使用 appendRow(),特别是当数据集变大时不推荐使用,第二种方法是使用 setValues(),效率更高。

第一个解决方案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]]); 
        }
  }
  

}

第二个解决方案 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);
}