我如何为我的所有帐户添加文档功能,当我打开新文档的文件时,它想找到我现在的文件夹+最后文件名并增加1并保存。
对于名为“无标题文档”的默认Google Doc保存文档,但我希望将其保存:
folder_1.1001.doc folder_1.1002.doc
如果我在下一个Folder2中创建文件:
folder_2.001.doc folder_2.002.doc。
我的坏代码:
// Show current folder name & root folder name
function makeFilename() {
// Get current folder name
var ui = DocumentApp.getUi();
thisFileId = DocumentApp.getActiveDocument().getId();
var thisFile = DriveApp.getFolderById(thisFileId);
var parentFolder = thisFile.getParents();
var currentFolderName = parentFolder.next();
ui.alert(currentFolderName);
// get all files in currentFolderName
var files = parentFolder;
while (files.hasNext()) {
var file = files.next();
// Logger.log(file.getName());
DocumentApp.getUi().alert(file.getName());
}
}
这是一个示例代码:
// Show current folder name
function makeFilename() {
// Get current file name
const ui = DocumentApp.getUi(),
doc = DocumentApp.getActiveDocument(), //Added
thisFileId = doc.getId(),
thisFileName = doc.getName();
const thisFile = DriveApp.getFileById(thisFileId);//Modified from getFolderById
const parentFolder = thisFile.getParents();
const currentFolder = parentFolder.next();//Modified from currentFolderName
const currentFolderName = currentFolder.getName();//Added
//ui.alert(currentFolderName);
/*Store a init file in root to getLatestFileNumber*/
var initIter = DriveApp.getFilesByName(currentFolderName + 'init000'),
initBool = initIter.hasNext(),
init;
if (!initBool) {
init = DriveApp.createFile(currentFolderName + 'init000', '0');
} else {
init = initIter.next();
}
/*Get current Number and format it to 4 digits*/
var currentNum = init.getBlob().getDataAsString() * 1 + 1,
formatNum = ('00000' + currentNum).substr(-4);
/*If filename already contains folderName, do nothing*/
if (!(thisFileName.search(currentFolderName) + 1)) {
doc.setName(currentFolderName + formatNum).saveAndClose();
init.setContent(currentNum);
}
}