1

I want to copy Spreadsheet file as new filename brought date ,

but makeCopy method always run first .

How can I let it run last or after a line ?

I tried:

1.take makeCopy method to another function and call it

2.add "while loop" before makeCopy method

3.use "if" judgment Spreadsheet updatetime then do makeCopy method

4.use sleep(60000) before makeCopy method

This is my code:

function DoCopyfile(){
    Copyfile(0);
    Copyfile(1);
}

function Copyfile(afterweek){

    var thissheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

    var weekstr = new Array("週一","週二","週三","週四","週五","週六","週日");

    for (var i=0;i<7;i++) {
    thissheet.getRange(1,2+i).setValue(Utilities.formatDate(new Date(new Date().setDate(new Date().getDate()-new Date().getDay()+1+7*afterweek+i)),"GMT+8","MM/dd") + " (" + weekstr[i] + ")");
    }

    var PreMonday = new Date(new Date().setDate(new Date().getDate()-new Date().getDay()+1+7*afterweek));
    var Monday = new Date(Utilities.formatDate(PreMonday,"GMT+8","yyyy/MM/dd 00:00:00"));
    var PreSunday = new Date(new Date().setDate(new Date().getDate()-new Date().getDay()+7+7*afterweek));
    var Sunday = new Date(Utilities.formatDate(PreSunday,"GMT+8","yyyy/MM/dd 23:59:59"));

    var allrange = thissheet.getRange("B2:H25");
    allrange.clear();
    allrange.breakApart();

    var cal = CalendarApp.getCalendarById("My Gmail");
    var events = cal.getEvents(Monday, Sunday, {search: ''});

    for (i=0;i<events.length;i++) {    
        var weekday = events[i].getStartTime().getDay();
        var StartRange = (events[i].getStartTime().getHours()-7)*2+events[i].getStartTime().getMinutes()/30;
        var EndRange = (events[i].getEndTime().getHours()-7)*2+events[i].getEndTime().getMinutes()/30;
        var currentrange = thissheet.getRange(StartRange,1+weekday,EndRange-StartRange,1);
        var when = Utilities.formatDate(events[i].getStartTime(),"GMT+8","hh:mm a")+"-"+Utilities.formatDate(events[i].getEndTime(),"GMT+8","hh:mm a");
        currentrange.merge();
        currentrange.setBorder(true, true, true, true, false, false);
        currentrange.setValue(events[i].getTitle()+"\n"+when);
    }

    var thisfolder = DriveApp.getFolderById("My google drive folder ID");
    var newfilename = "行事曆_" + Utilities.formatDate(Monday,"GMT+8","yyyy-MM-dd") + "_" + Utilities.formatDate(Sunday,"GMT+8","yyyy-MM-dd");
    var destfile = thisfolder.getFilesByName(newfilename);

    while (destfile.hasNext()){
        var file = destfile.next();
        file.setTrashed(true);   
    }

    var newfile = DriveApp.getFileById("My Spreadsheet file ID").makeCopy(newfilename,thisfolder);    

}
Jasn Hr
  • 31
  • 4
  • Oh! Thx! I have an idea that conver to xlsx file and sending to Line App with Google Apps Script at present. I just found way of conver to xlsx file, and I research how can sending to Line App, maybe use server with Line API. – Jasn Hr Aug 06 '18 at 02:31

1 Answers1

1

Sorry

A friend told me use Promise,

and I find SpreadsheetApp's method flush().

I add SpreadsheetApp.flush() before makeCopy and it working.

Jasn Hr
  • 31
  • 4