I'm trying to modify an XML file on my Google Drive using :
var file = DriveApp.getFilesByName('keyWordsList.xml').next();
var xml = file.getBlob().getDataAsString();
var document = XmlService.parse(xml);
var root = document.getRootElement();
var child = XmlService.createElement('keyword');
var childName = XmlService.createElement('label').setText(name);
root.addContent(child);
document = XmlService.createDocument(root);
xml = XmlService.getPrettyFormat().format();
DriveApp.createFile("keyWordsList.xml", xml);
This is a simplified example.
But XmlService.createDocument(root); throws an error :
Invalid argument: rootElement
I found a solution if someone get the same problem.
You dont need to create the document for being accept by Google Drive
var root = document.getRootElement();
var child= XmlService.createElement('keyword');
var childName = XmlService.createElement('label').setText(name);
var childKeyWord = child.addContent(childName);
for(var x=0;x<keywords.length;x++)
{
childKeyWord = XmlService.createElement('motcle').setText(keywords[x]);
child.addContent(childKeyWord);
}
root.addContent(child);
var xml = XmlService.getPrettyFormat().format(root);
DriveApp.createFile("keyWordsList.xml",xml,"text/xml");
Related
So I'm creating this code where the template is selected through a series of If statements. so it is a changing variable. I am simply trying to now replace text in the selected template and I keep getting the same error that getBody() is not a function. Any help is greatly appreciated!
var doc = DriveApp.getFileById(templateId);
var copy = doc.makeCopy();
var destination = DriveApp.getFolderById('1mGCx4yXX_NnLHsHsGWBGkzwAVhG-cTrc');
destination.addFile(copy);
copy.setName(regno + ' statistical analysis');
var copiedTemplateId = copy.getId();
var body = doc.getBody();
var header = doc.getHeader();
DriveApp.getFileById returns a File.
So, doc is of type File.
There is no "getBody" function for a File, at least it doesn't exist in the documentation: https://developers.google.com/apps-script/reference/drive/file
This works:
function myfunk() {
const regno = "test";
var file = DriveApp.getFileById("fileid");
var destination = DriveApp.getFolderById("folderid");
let name = regno + ' statistical analysis';
var copy = file.makeCopy(name, destination);
var copiedTemplateId = copy.getId();
let doc = DocumentApp.openById(copiedTemplateId);
var body = doc.getBody();
var header = doc.getHeader();
Logger.log('id: %s, body: %s, header: %s',copiedTemplateId,body,header);
}
File was a Google Document and a copied was created in the appropriate directory and renamed correctly.
Need help on simplifying this script. I still need to add more clusters in while loops.
function Files() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("AME");
var SupplierName = names.getRange(names.getLastRow(),5).getValue();
var Cluster = names.getRange(names.getLastRow(),3).getValue();
if (Cluster == 'US'){
var ClusterID = DriveApp.getFolderById("1z2R");
var newFolderID = ClusterID.createFolder(SupplierName);
var sourceFolder = DriveApp.getFoldersByName("US").next();
var files = sourceFolder.getFiles();
var destFolder = DriveApp.getFoldersByName(SupplierName).next();
while(files.hasNext()){
var file = files.next();
file.moveTo(destFolder);
}
return newFolderID.getId();
SUGGESTION:
We are still kind of lurking in the dark here but here are some opportunities I have identified on the provided script above.
/** Can be a global variable */
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("AME");
var SupplierName = names.getRange(names.getLastRow(),5).getValue();
var Cluster = names.getRange(names.getLastRow(),3).getValue();
function Files() {
if(Cluster == 'US') {
var ClusterID = DriveApp.getFolderById("SET FOLDER ID");
var folders = ClusterID.getFoldersByName(SupplierName).hasNext();
if(folders !== true){
ClusterID.createFolder(SupplierName);
moveFolders(SupplierName);
}
else {
moveFolders(SupplierName);
}
}
else {
Logger.log('Cluster is ' + Cluster);
}
}
function moveFolders() {
var sourceFolder = DriveApp.getFoldersByName("US").next();
var files = sourceFolder.getFiles();
Logger.log(files);
var destFolder = DriveApp.getFoldersByName(SupplierName).next();
while(files.hasNext()){
var file = files.next();
file.moveTo(destFolder);
}
}
Created a separate function moveFolders() to move files to the created folder ClusterID.createFolder(SupplierName); if Cluster 'US' was checked on the last row of the sheet. I assumed that your data is from a response that came from a Google Form that is why the last row is only being checked.
Sample Data:
Source parent folder for Cluster Folder and US Folder:
Files inside the "US" Folder:
After the script is run, created the SupplierName folder containing the files moved from the US Folder:
Now I haven't worked on dynamically adjusting the script to manually define the cluster name as of the moment due to lack of clarity of the question, but I'll modify my answer once questions are addressed.
I am pulling a drive activity report using GCP/GAMadv which gives me file IDs of various users in our workspace domain on a google sheet. My goal is to find the folder path of these files using file IDs. I am using appscript to get to that. Here is the code that I am running so far.
function getFolderPath(fileID, folderPath =""){
var sheetID = "1YfZgkLvAnPj7kOIQOVkcXeJgnh-KTecMn6er1a0elkk"
var sheet = SpreadsheetApp.openById(sheetID)
// var file = sheet.Files.get(fileID)
var parent = Drive.Files.get(fileID);
console.log(parent)
// console.log(file)
var parentElement = parent.items[0]
console.log(parentElement)
// var parentElement = parent[0]
var parentFile = Drive.Files.get(parent.id);
var parentPath = parentFile.title;
if (parent.isRoot)
return "/" + folderPath;
else {
return getFolderPath(
parentFile.id,
parentPath + "/" + folderPath
);
}
}
Looks like this is returning ALL the files we have in our drive rather than the ones on the sheet.
Help would be greatly appreciated! Thanks.
Folder Path from Id
function getFolderPathFromId(id="fileid") {
try {
var file = DriveApp.getFileById(id)
var pA = [];
pA.push(file.getName());
var folder = file.getParents();
while (folder.hasNext()) {
var f = folder.next();
pA.push(f.getName());
folder = f.getParents()
}
var r = pA.reverse().slice(0,-1).join(' / ');
}
catch (e) {
return e;
}
Logger.log(r);
return r;
}
I'm trying to get attachment from Gmail to Google Drive. After create a new file into the folder, I have encountered a problem accessing the new file that script created.
same as the makeCopy Function
err: Exception: The document is inaccessible. Please try again later. at myFunction(Code:21:37)
Appreciated any help and suggestions. Thank you!
function myFunction() {
const files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxx").getFiles();
while (files.hasNext()){
var file = files.next();
var filename = file.getName();
const aname = "xxxx.docx"
if (filename == aname){
var oldid = file.getId();
var copyfile = file.makeCopy().setName("COPYYYYY");
var newid = copyfile.getId();
}
}
var body = DocumentApp.openById(newid).getBody();
Logger.log(oldid);
Logger.log(newid);
Logger.log(body);
}
Modification points:
In your script, I think that when file of var copyfile = file.makeCopy().setName("COPYYYYY"); is Google Document, the script works. But from your error message and const aname = "xxxx.docx", I thought that you might try to have directly opened the DOCX file using DocumentApp.openById. If it's so, the reason of your issue is this.
In order to open the DOCX file using DocumentApp.openById, in the current stage, it is required to convert the DOCX file to Google Document.
When above points are reflected to your script, it becomes as follows.
Modified script:
Before you use this script, please enable Drive API at Advanced Google services.
function myFunction() {
const files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxx").getFiles();
var newid = "";
while (files.hasNext()){
var file = files.next();
var filename = file.getName();
const aname = "xxxx.docx";
if (filename == aname){
var oldid = file.getId();
if (file.getMimeType() == MimeType.MICROSOFT_WORD) {
var copyfile = Drive.Files.copy({title: "COPYYYYY"}, oldid, {convert: true});
newid = copyfile.id;
}
}
}
if (newid) {
var body = DocumentApp.openById(newid).getBody();
Logger.log(oldid);
Logger.log(newid);
Logger.log(body);
}
}
In this modified script, the DOCX file is converted by the method of "Files: copy" of Drive API.
Reference:
Files: copy
My remote js file as below
(function(window,document){
var config = {
'placeHolder' : 'content_div'
};
var html = "blaaa blaaa";
})(window,document);
I would likte to get value of html.
I have tried as below but I have got error.
string _JsVars = this._HttpWebRequestRemoteUrl(#"http://www.remotewebpage.com");
var engine = new Jurassic.ScriptEngine();
var result = engine.(_JsVars);
var var1 = engine.GetGlobalValue<string>("html");
MessageBox.Show(var1.ToString());