简体   繁体   中英

Google scripts copy range from one spreadsheet to another based on date

I am currently trying to automatically archive responses from a Google form. I have a script which runs every time the form is submitted and does some processing of the last submit.
What I want to do is on the first submit of the month, create a separate spreadsheet with just last months entries.
I am doing this by getting the date from last month, creating a new spreadsheet with last months name and year as the name of the file.

What I now need to do is select a range based on dates. So in column A is a timestamp (eg 31/12/2014 22:21:31) - I would want to select all rows between for example 1/12/2014 00:00:00 and 31/12/2014 23:59:59 .

Using this example I know its possible to copy a range but it's finding the correct range I need help with:

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0Aqv8.....");
  var source_sheet = ss.getSheetByName("Form Responses");
  var target_sheet = target.getSheetByName("Sheet1");
  var source_range = source_sheet.getRange("A2:A");
  var target_range = target_sheet.getRange("A2:A");

  var values = source_range.getValues();
  target_range.setValues(values);

I am using the following to get the start time, and end time of the month:

  var x = new Date();
  var d = new Date();

  // Start Time:
  x.setDate(1);
  x.setHours(0);
  x.setMinutes(0);
  x.setSeconds(0);
  x.setMonth(x.getMonth()-1);

  // Finish Time:
  d.setDate(0);
  d.setHours(23);
  d.setMinutes(59);
  d.setSeconds(59);

Ok so this probably isn't the best answer, but hey it seems to work for me:

var x = new Date();
var d = new Date();
// Start Time:
x.setDate(1);
x.setHours(0);
x.setMinutes(0);
x.setSeconds(0);
x.setMonth(x.getMonth()-1);
// Finish Time:
d.setDate(0);
d.setHours(23);
d.setMinutes(59);
d.setSeconds(59);
// Create a blank spreadsheet:
var root = DocsList.getRootFolder()
var newFileId = SpreadsheetApp.create(archiveName).getId();
var newFile = DocsList.getFileById(newFileId);
newFile.addToFolder(destFolder);
newFile.removeFromFolder(root);

// Get the spreadsheets:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById(newFileId);
var sheet = ss.getSheetByName("Form Responses");
var target_sheet = target.getSheetByName("Sheet1");

// Record the range:
var firstRow = 1;
var lastRow = 1;
var width = sheet.getDataRange().getWidth();

// Get all the current data:
var numRows = sheet.getDataRange().getNumRows();
var data = sheet.getDataRange().getValues();

// Get the first entry:
for(var i=0; i<numRows; i++){
  var tmp = new Date(data[i][0]);
  if (tmp > x) {
    firstRow = i+1;
    Logger.log(i + " - is the first row");
    i = numRows;
  }
}

// Get the last entry:
for(var i=0; i<numRows; i++){
  var tmp = new Date(data[i][0]);
  if (tmp > d) {
    lastRow = i;
    Logger.log(i + " - is the last row");
    i = numRows;
  }
}

// Copy the title:
var source_range = sheet.getRange(1,1,1,width);
var target_range = target_sheet.getRange(1,1,1,width);
var values = source_range.getValues();
var formats = source_range.getNumberFormats();
target_range.setValues(values);
target_range.setNumberFormats(formats);
target_sheet.setFrozenRows(1);

// Copy the last months values:
var source_range = sheet.getRange(firstRow,1,lastRow-firstRow+1,width);
var target_range = target_sheet.getRange(2,1,lastRow-firstRow+1,width);
var values = source_range.getValues();
target_range.setValues(values);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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