Script to save full Google Spreadsheet to folder - google-apps-script

I want to create a function to save a spreadsheet to a folder.
I'm almost there, I think I just need to adjust the synthax of the createFile class.
Here is my code:
const spreadsheet2 = SpreadsheetApp.getActiveSpreadsheet();
var folder2 = DriveApp.getFoldersById("my_folder_id").next();
var filename2 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("B97").getValue();
folder2.createFile([I think here is my missing command].setName(filename2));
Could someone finish it?

You want to copy the active Spreadsheet to the specific folder.
You want to use the filename from getActiveSheet().getRange("B97").getValue().
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification points:
At DriveApp.getFoldersById("my_folder_id").next(), there is no method of getFoldersById. When you want to use the folder ID, please use DriveApp.getFolderById(folderId).
Unfortunately, the Spreadsheet cannot be directly created with createFile(). In your case, in order to copy the active Spreadsheet, you can use makeCopy().
When above points are reflected to your script, it becomes as follows.
Modified script:
var folderId = "###"; // Please set the folder ID you want to put the copied Spreadsheet.
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var filename2 = spreadsheet.getActiveSheet().getRange("B97").getValue();
var folder = DriveApp.getFolderById(folderId);
DriveApp.getFileById(spreadsheet.getId()).makeCopy(filename2, folder);
References:
getFolderById()
makeCopy()

Related

Update Google Doc Files With Data Pulled From a SpreadSheet Using Doc File Name

I need to assign a membership number to each request I receive from aspiring members of my association.
Aspiring members fill a Google Docs file. When their requests are validated, those files need to be updated with the assigned membership number.
Inside the spreadsheet I have two columns. One contains the membership number and the other contains the ID of the request.
Spreadsheet Screenshot
The doc files are named with the ID of the request as well. The membership numbers should be added in place of the placeholder {membershipnumber} found in the doc files.
Doc File Screenshot
Is anyone able to help?
Thanks a lot for your precious help!
I really have no idea how to accomplish this.
I believe your goal is as follows.
You have Google Document files in the specific folder and have a Google Spreadsheet including the values for updating the Google Document files.
You want to replace the value of {membershipnumber} with the values of column "A" by searching the filename using the value of column "C".
In this case, how about the following sample script?
Sample script:
In this sample, please copy and paste the following script to the script editor of Google Spreadsheet and set the variables. And, save the script.
function myFunction() {
const folderId = "###"; // Please set your folder ID of the folder including the Google Document files.
const sheetName = "Sheet1"; // Please set your sheet name.
const folder = DriveApp.getFolderById(folderId);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const obj = sheet.getRange("A2:C" + sheet.getLastRow()).getDisplayValues().reduce((o, [a, , c]) => (o[c] = a.trim(), o), {});
const docs = folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
const file = docs.next();
const filename = file.getName().trim();
if (obj[filename]) {
const doc = DocumentApp.openById(file.getId());
doc.getBody().replaceText("{membershipnumber}", obj[filename]);
}
}
}
When this script is run, the Google Document files are updated using the values of column "A" by searching the Document files with the filename retrieved from column "C".
References:
reduce()
getFilesByType(mimeType)
replaceText(searchPattern, replacement)

Move folder matching a specific name into another folder?

Could someone provide me with a simple example how to use App Script to move a folder matching a specific name (not ID) into another folder inside Google Drive?
I have searched the existing threads but couldn't find anything specific to matching a specific folder name.
I believe your goal as follows.
You want to move a folder to other folder in your Google Drive using Google Apps Script.
For this, how about this answer? In your case, when the parent folder of the folder is moved, all files and sub folders in the folder can be moved. So the script can be simpler than the script for copying files. This has already been mentioned by Cooper's comment.
The sample script is as follows. As the input values, the source folder name and destination folder ID are used.
Sample script:
In this sample script, Drive service is used.
function myFunction() {
const sourceFolderName = "###"; // Please set the source folder name.
const destinationFolderId = "###"; // Please set the destination folder ID.
const src = DriveApp.getFoldersByName(sourceFolderName);
if (src.hasNext()) {
const srcFol = src.next();
srcFol.getParents().next().removeFolder(srcFol);
DriveApp.getFolderById(destinationFolderId).addFolder(srcFol);
}
}
Note:
When Drive API is used, the script becomes as follows.
function myFunction() {
const sourceFolderName = "###"; // Please set the source folder name.
const destinationFolderId = "###"; // Please set the destination folder ID.
const src = DriveApp.getFoldersByName(sourceFolderName);
if (src.hasNext()) {
Drive.Files.patch({parents: [{id: destinationFolderId}]}, src.next().getId());
}
}
References:
removeFolder(child)
addFolder(child)

Download file to google drive folder using app script

I am using this script to download a URL set in a google sheet cell to a specific folder in google drive called "game_thumb"
If cell B1 is "yyyyy.com/picture.png" I expect picture.png to be downloaded to the google drive folder.
I get an error"ReferenceError: "DocsList" not defined. (line 5).
I also would like the file to be renamed to include Cell A1
(contentCellA1_picture.png) before it is downloaded to drive.
The code I use:
function getFile(fileURL) {
// see https://developers.google.com/apps-script/class_urlfetchapp
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob()
var folder = DocsList.getFolder('game_thumb');
var result = folder.createFile(fileBlob);
debugger; // Stop to observe if in debugger
}
You want to save the downloaded file in the specific folder which has the name of game_thumb.
You want to set the value of cell "A1" to the filename of downloaded file.
If my understanding is correct, how about this modification?
For your question 1:
From your question, it is found that although I'm not sure whether the data is the file blob you want, the data from the URL can be retrieved. So in order to remove the error, please modify as follows.
From:
var folder = DocsList.getFolder('game_thumb');
To:
var folder = DriveApp.getFoldersByName('game_thumb').next();
In this modification, it supposes that the folder which has the name of game_thumb is only one in your Drive. If there are the folders with several same names, please tell me.
For your question 2:
Please modify as follows.
From:
var result = folder.createFile(fileBlob);
To:
var name = SpreadsheetApp.getActiveSheet().getRange("A1").getValue();
var result = folder.createFile(fileBlob).setName(name);
From your question, I'm not sure whether you are using the container-bound script or standalone script, and also I'm not sure where sheet there is the cell "A1" is. So this modification supposes that the cell "A1" of the active sheet is used.
References:
getFoldersByName()
getValue()
If I misunderstood your question and this modification didn't work, I apologize. At that time, can you provide the information of the situation?

Programmatically delete a script

I was wondering if someone could offer me some advice.
I have a master spreadsheet, acting as a template. I have written a script which can be run from the menu (using addToUi command), which makes a copy of template spreadsheet.
The problem is that the script gets copied into the new spreadsheet also which I don't want.
Could anyone suggest a possible way around this please?
I did think a possible way was to get the script to open the copied template and delete the script but not sure if this is possible.
Here is the function which does the copying....
function createCopy() {
var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("B8").getValue();
var destinationFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxx");
DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(myValue,destinationFolder);
}
(Cell reference B8 holds the value of what I called the copied spreadsheet).
Rayden, I use a function like that to just copy one sheet to a new spreadsheet and it doesn't drag the script with it. gDrive is the id for the Spreadsheet, tabName the individual sheet you want copied, Filename the name of the copy and destination the destination directory.
//******************************************************************************
//- This function takes a tab and makes it its own file
function tabToSheet(gDrive,tabName,fileName,destination){
var sh = SpreadsheetApp.openById(gDrive);
var ss = sh.getSheetByName(tabName);
//create a new document in the location given
var newSheet = SpreadsheetApp.create("TEMPDELETEME");
//copy the tab from current document to new document
ss.copyTo(newSheet);
var id = newSheet.getId();
newSheet.deleteSheet(newSheet.getSheetByName("Sheet1"));
os = newSheet.getSheets()[0];
os.setName(tabName);
var file = DriveApp.getFileById(id);
var folder = DriveApp.getFolderById(destination);
var finalId = file.makeCopy(fileName, folder).getId();
file.setTrashed(true);
return finalId;
}//*****************************************************************************
The difference is that i'm making a new sheet, then copying the tab rather than copying the entire file. You could add another tab and remove the variables if you want to copy multiple tabs.
I was having trouble implementing J.G.'s approach of moving individual sheets, so the approach I took was to add a conditional in front of the script to only run if the spreadsheet id was equal to the id of the original spreadsheet. My use case was trying to suppress a custom menu on the original workbook from reappearing on the copy, so it worked for that.
var bookId = spreadsheet.getId();
if (bookId === 'original spreadsheet id') {
*Function*
}

How can copy specifics sheet to another spreadsheet using google script

Situation:
I have a spreadsheet with 10 worksheets and 15 users logging in and modifying it. I want to copy just 6 sheet from other spreadsheet.
Script Function Needed:
The script should be copy sheet to another spreadsheet specific.
1)
Source = XXXXXX
I want to copy from Source only the sheets1,2,3,4,5,6,7,8 to target.
Target = YYYYY
Note: The Target already have the sheet1,2,3,4,5,6,7,8. I needs update that Target spreadsheet because I use that to make Reports. I can not use the surce because this report are confidential.
2)
The script needs to be run from the target.
Problem:
I read a lot of forum but anyone provide a generic script about "How to Copy Sheets to anothers Sheet". All those script are very complex an I'm newbe with this.
Test Case:
N/A
I thinks is a simple copy of sheet from one spreadsheet to another but I needs to do this in oneshot actually I am doing it manually but I have issue the system change the sheet name to Copy of XXXXX but my Reports are configured to use a specific name, that why I needs make this possible. I thinks this script will help to many people.
Thanks in Advance.
EDIT: 14/06/2013
Hi,
Please could you help me to tunning this script to work more effective.
function UpdateData() {
var source = SpreadsheetApp.openById('YYYYYY');
var sheet8 = source.getSheets()[8];
var sheet7 = source.getSheets()[7];
var sheet6 = source.getSheets()[6];
var sheet5 = source.getSheets()[5];
var sheet4 = source.getSheets()[4];
var sheet3 = source.getSheets()[3];
var sheet2 = source.getSheets()[2];
var sheet1 = source.getSheets()[1];
var destination = SpreadsheetApp.openById('ZZZZZZ');
sheet8.copyTo(destination).setName('Sheet8');
sheet7.copyTo(destination).setName('Sheet7');
Utilities.sleep(100);
sheet6.copyTo(destination).setName('Sheet6');
sheet5.copyTo(destination).setName('Sheet5');
Utilities.sleep(100);
sheet4.copyTo(destination).setName('Shee4');
sheet3.copyTo(destination).setName('Shee3');
Utilities.sleep(100);
sheet2.copyTo(destination).setName('Shee2');
sheet1.copyTo(destination).setName('Shee1');
SpreadsheetApp.flush();
}
just look into the docs: copyTo(spreadsheet), from the description:
Copies the sheet to another spreadsheet. The destination spreadsheet
can be the source. The new spreadsheet will have the name "Copy of
[original spreadsheet name]"
The method returs the copied sheet so you can rename it:
sourceSheet.copyTo(targetSpreadSheet).setName('somename');
I hope, you know how to write a loop in JS ...