Skip to content

Instantly share code, notes, and snippets.

@CodingKoopa
Created August 18, 2022 02:39
Show Gist options
  • Save CodingKoopa/241f7db9557d7db524a2dbdb992d1797 to your computer and use it in GitHub Desktop.
Save CodingKoopa/241f7db9557d7db524a2dbdb992d1797 to your computer and use it in GitHub Desktop.
something google script
// Entrypoint function.
function driveHelper() {
// Get a collection of all files in the user drive.
const files = DriveApp.getFiles();
// For each file.
while (files.hasNext()) {
// Get the file.
const file = files.next();
// Fix restricted characters (https://rclone.org/local/#restricted-characters)
_fixForbiddenCharacters(file);
// Convert Open Office XML to Docs Editor (disabled).
// _convertOpenOfficeXml(file);
}
}
function _fixForbiddenCharacters(file) {
if (file === undefined)
throw new Error(`No file passed. Did you accidentally run this function directly?`);
var name = file.getName();
var doUpdate = false;
if (name.includes(`␀`)) {
Logger.log(`Replacing ␀ in "${_getPath(file)}".`);
name = name.replaceAll(`␀`, "\0");
doUpdate = true;
}
if (name.includes(`/`)) {
Logger.log(`Replacing / in "${_getPath(file)}".`);
name = name.replaceAll(`/`, "/");
doUpdate = true;
}
// Only update if we need to, to limit API calls.
if (doUpdate)
file.setName(name);
}
function _convertOpenOfficeXml(file) {
if (file === undefined)
throw new Error(`No file passed. Did you accidentally run this function directly?`);
// Get the file extension.
const extension = file.getName().split(`.`).pop();
// If this is an Open Office XML file that we can convert (disabled)
if (extension == `docx` || extension == `pptx` || extension == `xlsx` ) {
Logger.log(`Migrating "${_getPath(file)}" to Docs Editor format.`);
// Create a new file using the Advanced Drive Service.
const fileConv = Drive.newFile();
// Set the title (name) of the file.
fileConv.setTitle(file.getName().replace(`.${extension}`, ``))
// Set the parent folders of the file.
// This doesn't work unfortunately, not sure why.
// fileConv.setParents(file.getParents());
// Get the document data.
const blob = file.getBlob();
// Initialize the new file with the converted document.
var conversionAdv;
try {
conversionAdv = Drive.Files.insert(fileConv, blob, {convert: true});
}
catch(error) {
Logger.log(`Error: "${error}". Skipping this file.`);
return;
}
// If this file has a parent.
if (file.getParents().hasNext())
// At this point, we've created a new file, and it has the correct name and contents. It's
// not in the correct folder however. We couldn't use the built-in Drive service to set this
// up earlier on, nor can we use the Advanced Drive Service API to fix this (at least, I
// couldn't get it working). So, now we have to bring it *back* to the original built-in
// Drive service API to fix this.
file.getParents().next().addFile(DriveApp.getFileById(conversionAdv.id))
// Trash the original file now that we've converted it.
file.setTrashed(true);
}
}
// Gets the path of a file within a drive. There is no leading "/".
function _getPath(file) {
if (file === undefined)
throw new Error(`No file passed. Did you accidentally run this function directly?`);
return _formatParents(file.getParents()).substring(1) + "/" + file.getName();
}
function _formatParents(parents) {
if (parents === undefined)
throw new Error(`No parent passed. Did you accidentally run this function directly?`);
if (parents.hasNext()) {
const parent = parents.next();
const name = parent.getName();
if (name !== `My Drive`)
return _formatParents(parent.getParents()) + "/" + parent.getName();
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment