Every day there is a new report that I want to copy to a master sheet outside of the folder, I'm using a different script to transfer the file from my Gmail to a folder in Drive, there is only one file at a time in the folder. I tried getting the id of the file with this code and then use the id to copy and paste the info. I'm still trying to understand how google scripts works so I know the code is not functional, I would like to know if what I want to do is possible and a guide to what I could do.
function obtainId() {
var folder = DriveApp.getFolderById('ID');
var files = folder.getFiles();
var file = files.next();
var id = DriveApp.getFileById(file.getId());
var ss = SpreadsheetApp.getActiveSpreadsheet(id);
var copySheet = ss.getSheetByName("name");
var pasteSheet = ss.getSheetByName("name");
var source = copySheet.getRange(range);
var destination = pasteSheet.getRange(range);
source.copyTo(destination);
}
CodePudding user response:
Obtain Id copy data for a file with today's date in particular folder
function obtainId() {
let dt = new Date();
let tdv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();
var folder = DriveApp.getFolderById('ID');
var files = folder.getFilesByType(mimeType.GOOGlE_SHEETS);
let id;
while (files.hasNext()) {
let file = files.next(;
if (file.getDateCreated().valueOf() > tdv) {
id = file.getId();
break;
}
}
const ss = SpreadsheetApp.openById(id);
const csh = ss.getSheetByName("copy sheet name");
const psh = ss.getSheetByName("paste sheet name");
csh.getRange().copyTo(psh.getRange();)
}
Using Sheets API:
function obtainId() {
let dt = new Date();
let tdv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()).valueOf();
var folder = DriveApp.getFolderById(gobj.globals.folder3id);
var files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);
let id;
while (files.hasNext()) {
let file = files.next();
if (file.getDateCreated().valueOf() > tdv) {
id = file.getId();
break;
}
}
const ss = SpreadsheetApp.openById(id);
const csh = ss.getSheetByName("Sheet1");
const dss = SpreadsheetApp.getActive();
const psh = dss.getSheetByName("Sheet2");
let crg = csh.getDataRange().getA1Notation();
let vs = Sheets.Spreadsheets.Values.get(ss.getId(),`${csh.getName()}!${crg}`).values;
prg = psh.getRange(psh.getLastRow() 1,1,vs.length,vs[0].length).getA1Notation();
Sheets.Spreadsheets.Values.update({values: vs}, dss.getId(), psh.getSheetName(), {valueInputOption: "USER_ENTERED"});
}