google-apps-script
DriveApp Service - Fichiers par type et chaîne de recherche
Recherche…
Paramètres
Le nom du paramètre | Utiliser pour |
---|---|
searchString | la chaîne à trouver dans le nom du fichier |
Récupère les fichiers par type de fichier avec la chaîne correspondante dans le nom du fichier
Obtenez tous les formulaires Google avec le mot "Sans titre" dans le nom du fichier.
function mainSearchFunction(searchStr) {
var fileInfo,arrayFileIDs,arrayFileNames,arrayOfIndexNumbers,
allFileIDsWithStringInName,i,searchStr,thisID;//Declare variables
if (!searchStr) {
searchStr = "Untitled";//Assign a string value to the variable
};
fileInfo = getFilesOfType();//Run a function that returns files information
arrayFileNames = fileInfo[1];//Get the array of file names
arrayOfIndexNumbers = searchFileNamesForString(arrayFileNames,searchStr);
//Logger.log('searchStr: ' + searchStr)
//Logger.log(arrayOfIndexNumbers)
allFileIDsWithStringInName = [];
arrayFileIDs = fileInfo[0];
for (i=0;i<arrayOfIndexNumbers.length;i+=1) {
thisID = arrayFileIDs[arrayOfIndexNumbers[i]];
allFileIDsWithStringInName.push(thisID);
};
Logger.log(allFileIDsWithStringInName)
};
function getFilesOfType() {
var allFormFiles,arrFileName,arrFileID,arrFileUrls,thisFile;
allFormFiles = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
arrFileName = [];
arrFileID = [];
arrFileUrls = [];
while (allFormFiles.hasNext()) {
thisFile=allFormFiles.next();
arrFileName.push(thisFile.getName());
arrFileID.push(thisFile.getId());
arrFileUrls.push(thisFile.getUrl());
};
//Logger.log(arrFileName)
return [arrFileID,arrFileName];
};
function searchFileNamesForString(arrayFileNames,searchStr) {
var arrayIndexNumbers,i,L,thisName;
arrayIndexNumbers = [];
L = arrayFileNames.length;
for (i=0;i<L;i+=1){
thisName = arrayFileNames[i];
Logger.log(thisName);
Logger.log('thisName.indexOf(searchStr): ' + thisName.indexOf(searchStr));
if (thisName.indexOf(searchStr) !== -1) {
arrayIndexNumbers.push(i);
};
};
return arrayIndexNumbers;
};
Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow