I have files on the folder and i import them to the google sheet.
I need to add filename to first column before import, but my script add it only to first cell before import
So it gives me:
Filename
Pencil
01/01/2022
Pen
01/02/2022
Hat
And i need
Filename
1/01/2022
Pencil
Filename
1/01/2022
Pen
Filename
01/02/2022
Hat
function Import_Kredo() {
var arr = [];
var files = DriveApp.getFolderById("13kymSiqX0-L6a9ev4It1_BEjH-xM8rnc").getFiles();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Table 1");
copySheet.getRange('A2:G').clear();
while (files.hasNext()) {
var file = files.next();
var mimeType = file.getMimeType();
var ss1 = null, id = null;
if (mimeType == MimeType.MICROSOFT_EXCEL) {
id = Drive.Files.copy({ mimeType: MimeType.GOOGLE_SHEETS }, file.getId()).id;
ss1 = SpreadsheetApp.openById(id);
} else if (mimeType == MimeType.GOOGLE_SHEETS) {
ss1 = SpreadsheetApp.open(file);
}
if (!ss1) continue;
var sheet = ss1.getSheetByName("Table 1");
if (!sheet) continue;
var sheetsRange = sheet.getDataRange();
var sheetsValues = sheetsRange.getValues().filter(r => r[1]).filter(e => e);
var v = sheetsValues.slice(1);
console.log([file.getName(), v])
arr = arr.concat([[file.getName(), ...Array(v[0].length - 1).fill(null)], ...v]);
if (id) DriveApp.getFileById(id).setTrashed(true); // or Drive.Files.remove(id);
}
copySheet.getRange(5, 1, arr.length, arr[0].length).setValues(arr);
}
In your situation, how about the following modification?
From:
arr = arr.concat([[file.getName(), ...Array(v[0].length - 1).fill(null)], ...v]);
To:
var filename = file.getName();
arr = arr.concat(v.map(e => [filename, ...e]));
By this modification, the filename is put into the 1st column of each row.
Related
please refer to the code below:
function getGdriveLinks() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Test');
const fileNames = sheet.getRange('C2:C').getValues();
const mainFolder = DriveApp.getFolderById('Folder ID');
for (var i = 0; i < fileNames.filter(String).length; i++){
var fileName = fileNames[i][0].toString();
var fileLink = getGdriveLink(fileName,mainFolder);
Logger.log(fileLink);
}
}
function getGdriveLink(searchFileName,folder){
var mainFiles = folder.getFiles();
while (mainFiles.hasNext()){
var file = mainFiles.next();
if(file.getName().indexOf(searchFileName) != -1){
Logger.log(file.getUrl());
return file.getUrl();
}
}
var subFolders = folder.getFolders();
while (subFolders.hasNext()){
var subFolder = subFolders.next();
getGdriveLink(searchFileName,subFolder);
}
}
Basically, the aim is to get the filenames in C2:C of Test sheet, find each of these in the 'Folder ID', and then return their URL. The URL is returned correctly when I log it from getGdriveLink(searchFileName,folder) using Logger.log(file.getUrl()). However, it returns null via the Logger.log(fileLink) when the result goes back to getGdriveLinks() function. Is there anything I'm missing? We're using a corporate domain if that's something to consider. Hoping for your guidance. Thanks!
You could also do it this way and avoid recursion altogether
function getPathAllDrivesFromId(fileid) {
var ids = [{id:fileid,name:DriveApp.getFileById(fileid).getName()}];
let r;
do {
r = Drive.Files.get(fileid,{supportsAllDrives:true,supportsTeamDrives:true});
if(r.parents.length > 0) {
//Logger.log(JSON.stringify(r.parents[0]));
ids.push({id:r.parents[0].id,name:DriveApp.getFolderById(r.parents[0].id).getName()});
fileid = r.parents[0].id
}
}while (r.parents.length > 0);
if(ids[ids.length - 1].name == "Drive") {
ids[ids.length - 1].name = Drive.Drives.get(ids[ids.length - 1].id).name;
}
//Logger.log(JSON.stringify(ids));
let path = ids.map(obj => obj.name).flat().reverse().join(' / ')
//Logger.log(path);
return path;
}
function getGdriveLinks() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Test');
const osh = ss.getSheetByName("Results");
const ns = sh.getRange('C2:C' + sh.getLastRow()).getValues();
const folder = DriveApp.getFolderById('Folder ID');
const result = [["Filename", "Id", "Url", "path"]];
ns.forEach(n => {
let files = DriveApp.getFilesByName(n);
while (files.hasNext()) {
let file = files.next();
let path = getPathAllDrivesFromId(file.getId());
if (~path.indexOf(folder.getName())) {
result.push([file.getName(), file.getId(), file.getUrl(), path]);
}
}
})
osh.clearContents();
osh.getRange(1, 1, result.length, result[0].length).setValues(result);
}
As part of a student attendance system, I would like to add a color stripe to every last row of a class for attendance using App Scripts. My columns of Google Sheets are: (i) Date, (ii) Email, (iii) Latitude, (iv) Longitude, and (v) Subject-code. Tried many ways but did not find the solution.
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ssID = sss.getId();
var sheetName = sss.getName();
var sheet = sss.getSheetByName("TempDataSet");
var sheet1 = sss.insertSheet('TempDataSet_temp');
sheet.getDataRange().copyTo(sheet1.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sheet.getDataRange().copyTo(sheet1.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
var shID = sheet1.getSheetId().toString();
sheet1.getRange(2, 1, sheet.getLastRow() -1, sheet.getLastColumn()).sort({column: 1, ascending: false});
var columns_delete = [7,2]; //[7,5,4,2];
columns_delete.forEach(col=>sheet1.deleteColumn(col));
//const sss = SpreadsheetApp.getActiveSpreadsheet();
//const sheet = sss.getSheetByName("TempDataSet");
const subs = sheet.getRange('F2:F'+sheet.getLastRow()).getValues().flat();
const usubs = subs.filter((value, index, self)=>self.indexOf(value) === index);
const dts = sheet.getRange('A2:A'+sheet.getLastRow()).getDisplayValues().flat();
const udts = dts.filter((value, index, self)=>self.indexOf(value) === index);
if(usubs.length>1){
subs.forEach((s,i)=>{
if(i>1){
if(subs[i]!=subs[i-1]){
sheet.getRange(i+1,1,1,5).setBackground('yellow');
}}});
}
else if (udts.length>1){
dts.forEach((d,i)=>{
if(i>1){
if(dts[i]!=dts[i-1]){
sheet.getRange(i+1,1,1,5).setBackground('yellow');
}}});
}
var from = Session.getActiveUser().getEmail();
var subject = 'Batch Attendance Record for Your Reference';
var body = 'Dear Student,'+ '\n\n' + 'Greetings! Please find the batch attendance record attached. Stay safe and blessed.' + '\n\n' + 'Thank you.';
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID+"&gid="+shID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
sss.deleteSheet(sss.getSheetByName('TempDataSet_temp'));
var sheet2 = sss.getSheetByName('StudentList');
var data = sheet2.getLastRow();
var students = [];
var students = sheet2.getRange(2, 6, data).getValues();
//MailApp.sendEmail(students.toString(), subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
for (var i=0; i<students.length; i++){ // you are looping through rows and selecting the 1st and only column index
if (students[i][0] !== ''){
MailApp.sendEmail(students[i][0].toString(), subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
//MailApp.sendEmail(students[i][0].toString(), subject ,body, {from: from, attachments:[{fileName:"YourAttendaceRecord.xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
}
}
Explanation:
Based on your question, I understand the following steps:
Check if you have at least two unique subjects in column E. One way to do that is to find the unique list of subjects. If the length of that list is 2 or more it means that you have different subjects. In that case, the first block of the if statement evaluates to true and you add a yellow line in the row before the subject is changed.
If you have only one subject, namely the length of the unique list of subjects is 1 the first block of the if statement will evaluate to false. In that case, the script will check whether column A has 2 or more unique dates. If it does, the second block of the if statement will be executed and the script will add a yellow line in the row before the date is changed. Otherwise, it won't do anything.
Solution:
You can execute color() as a standalone script. I would advice you to save this function in a new .gs file and then simply call it within your current script. Namely, put color() anywhere you want in the code snippet you provided.
function color() {
const sss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = sss.getSheetByName("TempDataSet");
const subs = sheet.getRange('E2:E'+sheet.getLastRow()).getValues().flat();
const usubs = subs.filter((value, index, self)=>self.indexOf(value) === index);
const dts = sheet.getRange('A2:A'+sheet.getLastRow()).getDisplayValues().flat();
const udts = dts.filter((value, index, self)=>self.indexOf(value) === index);
if(usubs.length>1){
subs.forEach((s,i)=>{
if(i>1){
if(subs[i]!=subs[i-1]){
sheet.getRange(i+1,1,1,5).setBackground('yellow');
}}});
}
else if (udts.length>1){
dts.forEach((d,i)=>{
if(i>1){
if(dts[i]!=dts[i-1]){
sheet.getRange(i+1,1,1,5).setBackground('yellow');
}}});
}
}
Complete Solution:
function sendEmails(){
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ssID = sss.getId();
var sheetName = sss.getName();
var sheet = sss.getSheetByName("TempDataSet");
var sheet1 = sss.insertSheet('TempDataSet_temp');
sheet.getDataRange().copyTo(sheet1.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
sheet.getDataRange().copyTo(sheet1.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
var shID = sheet1.getSheetId().toString();
sheet1.getRange(2, 1, sheet.getLastRow() -1, sheet.getLastColumn()).sort({column: 1, ascending: true});
var columns_delete = [7,2]; //[7,5,4,2];
columns_delete.forEach(col=>sheet1.deleteColumn(col));
SpreadsheetApp.flush();
const subs = sheet1.getRange('E2:E'+sheet1.getLastRow()).getValues().flat();
const usubs = subs.filter((value, index, self)=>self.indexOf(value) === index);
const dts = sheet1.getRange('A2:A'+sheet1.getLastRow()).getDisplayValues().flat();
const udts = dts.filter((value, index, self)=>self.indexOf(value) === index);
if(usubs.length>1){
subs.forEach((s,i)=>{
if(i>1){
if(subs[i]!=subs[i-1]){
sheet1.getRange(i+1,1,1,5).setBackground('yellow');
}}});
}
else if (udts.length>1){
dts.forEach((d,i)=>{
if(i>1){
if(dts[i]!=dts[i-1]){
sheet1.getRange(i+1,1,1,5).setBackground('yellow');
}}});
}
SpreadsheetApp.flush();
var from = Session.getActiveUser().getEmail();
var subject = 'Batch Attendance Record for Your Reference';
var body = 'Dear Student,'+ '\n\n' + 'Greetings! Please find the batch attendance record attached. Stay safe and blessed.' + '\n\n' + 'Thank you.';
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID+"&gid="+shID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
sss.deleteSheet(sss.getSheetByName('TempDataSet_temp'));
var sheet2 = sss.getSheetByName('StudentList');
var data = sheet2.getLastRow();
var students = [];
var students = sheet2.getRange(2, 6, data).getValues();
//MailApp.sendEmail(students.toString(), subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
for (var i=0; i<students.length; i++){ // you are looping through rows and selecting the 1st and only column index
if (students[i][0] !== ''){
MailApp.sendEmail(students[i][0].toString(), subject ,body, {attachments:[{fileName:sheetName+".xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
//MailApp.sendEmail(students[i][0].toString(), subject ,body, {from: from, attachments:[{fileName:"YourAttendaceRecord.xlsx", content:contents, mimeType:"MICROSOFT_EXCEL"}]});
}
}
}
Problem 1:
When I try to execute the below code it comes up with the error 'Exception: Invalid argument: name (line 37, file "Create New Project")'. I originally thought that it could be that the variable 'jobNumber' wasn't a string but now that it is, it still doesn't execute.
Problem 2:
When I don't use the variable 'jobNumber' for the folder name and just us a string value, it executes. However the new folder does not go into the source folders drive but into my personal drive.
function createProject(){
searchProject();
createFolder();
}
//Search for New Project and return values
function searchProject(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var search = "New Job";
for(var i = 0; i<data.length;i++){
if(data[i][9] == search){
Logger.log((i+1));
var jobNumber = sheet.getRange((i+1), 1).getDisplayValue()
var jobSite = sheet.getRange((i+1), 2).getDisplayValue()
var jobTitle = sheet.getRange((i+1), 3).getDisplayValue()
return i+1;
}
}
}
//Create New Project Folders
function createFolder(jobNumber, jobSite, jobTitle) {
var sourceFolder = "12345_Site Name_Commissioning Engineer Name";
var targetFolder = jobNumber
var source = DriveApp.getFoldersByName(sourceFolder);
var target = DriveApp.createFolder(targetFolder);
if (source.hasNext()) {
copyFolder(source.next(), target);
}
}
function copyFolder(source, target) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
file.makeCopy(file.getName(), target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var targetFolder = target.createFolder(folderName);
copyFolder(subFolder, targetFolder);
}
}
I have managed to find my own solution.
By adding the 'foler_ID' and 'folder' variable to get the folder to go into the correct folder in google drive.
I also missed calling the creatFolder function in searchProject function, with that now included the variables are carried over.
Thanks for all the contributors that helped :)
/**
* Create New Project
*/
//Search for New Project and return values
function createProject(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var search = "New Job";
for(var i = 0; i<data.length;i++){
if(data[i][9] == search){
Logger.log((i+1));
var jobNumber = sheet.getRange((i+1), 1).getDisplayValue()
var jobSite = sheet.getRange((i+1), 2).getDisplayValue()
var jobTitle = sheet.getRange((i+1), 3).getDisplayValue()
createFolder (jobNumber, jobSite, jobTitle);
Logger.log(jobNumber);
Logger.log(jobSite);
Logger.log(jobTitle);
return i+1;
}
}
}
//Create New Project Folders
function createFolder(jobNumber, jobSite, jobTitle) {
var sourceFolder = "12345_Site Name_Commissioning Engineer Name";
var targetFolder = (jobNumber + " " + jobSite + " " + jobTitle + "TESTING");
var foler_ID = '14QpCYc803GnDC_pR-7AqViePHBKyNNv_';
var folder = DriveApp.getFolderById(foler_ID);
var source = DriveApp.getFoldersByName(sourceFolder);
var target = folder.createFolder(targetFolder);
Logger.log(target);
if (source.hasNext()) {
copyFolder(source.next(), target);
}
}
function copyFolder(source, target) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
file.makeCopy(file.getName(), target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var targetFolder = target.createFolder(folderName);
copyFolder(subFolder, targetFolder);
}
}
I am trying to append all spreadsheets from the same folder to a master sheet.
I have found a similar code here but I can append the spreadsheets only when the master sheet is also in the selected folder. How could it be modified so that it could be able to applicable to other folders?please advise~
function appendData() {
var sheet = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById("1UQ5J3xdNnqyHvC-CJ1vAu5YPSxq9Sya9");
var files = folder.getFiles();
var cnt=0;
while (files.hasNext()) {
var file = files.next();
cnt++;
// if the file is not a Sheet, skip it
if (file.getMimeType() != "application/vnd.google-apps.spreadsheet") {
continue;
}
var importSheet = SpreadsheetApp.openById(file.getId())
// assuming you only want to append the first sheet of each Spreadsheet:
var range = importSheet.getSheets()[0].getDataRange();
// extend the main sheet if you need to:
try {
sheet.getRange(1, 1, sheet.getDataRange().getNumRows() + range.getNumRows(), 1);
}
catch (e) {
sheet.insertRowsAfter(sheet.getDataRange().getNumRows(), range.getNumRows() + 1);
}
// remove the headers from the data to copy:
range = importSheet.getSheets()[0].getRange(2, 1, range.getNumRows() - 1, range.getNumColumns())
// copy the data:
sheet.getRange(sheet.getDataRange().getNumRows() + 1, 1, range.getNumRows(), range.getNumColumns()).setValues(range.getValues());
}
}
Try this:
function appendData() {
var sheet=SpreadsheetApp.getActiveSheet();
var folder=DriveApp.getFolderById("1UQ5J3xdNnqyHvC-CJ1vAu5YPSxq9Sya9");
var files=folder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
var file = files.next();
var importSheet=SpreadsheetApp.openById(file.getId())
var values=importSheet.getSheets()[0].getDataRange().getValues();
sheet.getRange(sheet.getLastRow()+1,1,values.length,values[0].length).setValues(values);
Logger.log(file.getName());
}
}
Append both spreadsheets and Excel files to the same master sheet
If you need to append not only sheets from Google spreadsheets, but also form Excel files, you need to convert the latter ones to Google Sheets first.
For this, you can modify your existing code as following:
function appendData() {
var sheet = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById("1fUpTphVpYGfLv6SjWxZdbcIK7PHCZOnt");
var files = folder.getFiles();
var cnt=0;
while (files.hasNext()) {
var file = files.next();
cnt++;
// if the file is an excel - convert it to sheet
if (file.getMimeType() != "application/vnd.google-apps.spreadsheet") {
Logger.log(file.getMimeType());
//here is the additional part
//define here mime-types of interest and convert them
if (file.getMimeType() == "application/vnd.ms-excel" || file.getMimeType() == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
var fileId = file.getId();
var folderId = folder.getId();
var blob = file.getBlob();
var resource = {
title: file.getName(),
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{id: folderId}],
};
var file = Drive.Files.insert(resource, blob);
}else{
continue;
}
}
var importSheet = SpreadsheetApp.openById(file.getId())
// assuming you only want to append the first sheet of each Spreadsheet:
var range = importSheet.getSheets()[0].getDataRange();
// extend the main sheet if you need to:
try {
sheet.getRange(1, 1, sheet.getDataRange().getNumRows() + range.getNumRows(), 1);
}
catch (e) {
sheet.insertRowsAfter(sheet.getDataRange().getNumRows(), range.getNumRows() + 1);
}
// remove the headers from the data to copy:
range = importSheet.getSheets()[0].getRange(2, 1, range.getNumRows() - 1, range.getNumColumns())
Logger.log("range.getA1Notation():" + range.getA1Notation());
// copy the data:
sheet.getRange(sheet.getDataRange().getNumRows() + 1, 1, range.getNumRows(), range.getNumColumns()).setValues(range.getValues());
}
}
This is part of the html code from "view page source" I want to scrape
<span>Loyalty cijena</span>
<strong>863,84 KN</strong>
This is my code to scrape part which is now "863" and start my function if that number is under 863. But now the function start no matter condition is set [0-862] or [0-864]. Where is my mistake?
function createBackup() {
var folder = getFolder(FOLDER_NAME);
var exportUrl = RESOURCE_URL;
var response = UrlFetchApp.fetch(exportUrl);
var htmlBody = response.getContentText();
var scraped = htmlBody.match(/Loyalty cijena<\/span>\s*<strong>(\d+),(\d+) KN<\/strong>/m)[1];
if(scraped.match(/[0-862]/)); {
createBackupFile(folder, FILE_NAME, fetchData());
}
}
Edit: This is full code
var RESOURCE_URL = 'https://www...',
BACKUP_FOLDER_ID = 'xxx',
FOLDER_NAME_FORMAT = 'yyyy-MM-dd',
FILE_NAME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss",
FILE_EXT = '.pdf',
now = new Date(),
FOLDER_NAME = Utilities.formatDate(now, 'CET', FOLDER_NAME_FORMAT),
FILE_NAME = Utilities.formatDate(now, 'CET', FILE_NAME_FORMAT) + FILE_EXT;
function createBackup() {
var folder = getFolder(FOLDER_NAME);
var exportUrl = RESOURCE_URL;
var response = UrlFetchApp.fetch(exportUrl);
var htmlBody = response.getContentText();
var scraped = htmlBody.match(/Loyalty cijena<\/span>\s*<strong>(\d+),(\d+) KN<\/strong>/m)[1];
console.log(scraped,parseInt(scraped) > 0 && parseInt(scraped) < 863); {
createBackupFile(folder, FILE_NAME, fetchData());
}
}
function getFolder(name) {
var backupFolder = getBackupFolder(),
folders = backupFolder.getFoldersByName(name);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = backupFolder.createFolder(name);
}
return folder;
}
function getBackupFolder() {
return DriveApp.getFolderById(BACKUP_FOLDER_ID);
}
function createBackupFile(folder, filename, data, overwrite) {
if (overwrite) {
var existingFiles = folder.getFilesByName(filename);
while (existingFiles.hasNext()) {
var file = existingFiles.next();
folder.removeFile(file);
}
}
}
function fetchData() {
var exportUrl = RESOURCE_URL;
var response = UrlFetchApp.fetch(exportUrl);
var htmlBody = response.getContentText();
var blob = Utilities.newBlob(htmlBody, 'text/html').getAs('application/pdf').setName(FILE_NAME);
return folder.createFile(blob);
}
The pattern [0-862] matches text that ends with 62 and where the first symbol is 0 to 8, so for example 562 but not 561. In your case you should compare the number represented by those characters, instead of matching against a regular expression.
Don't use regex to match a value:
var htmlBody = `<span>Loyalty cijena</span>
<strong>863,84 KN</strong>`
var scraped = htmlBody.match(/Loyalty cijena<\/span>\s*<strong>(\d+),(\d+) KN<\/strong>/m)[1];
console.log(scraped,
parseInt(scraped) > 0 && parseInt(scraped) <= 863); // false if you change to < 863