如何通过在 google 应用程序脚本中使用其名称来获取 Gsheet 文件的 ID
How to I get the ID of a Gsheet file by using its name in google apps script
我正在拔头发。我有以下代码,并且一直在尝试弄清楚如何使用文件名获取 gsheet 文件的 ID。我的目标是按名称查找文件,然后获取 ID,然后从该文件中获取一个范围并将其输出到活动 sheet
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Master Analysis Functions')
.addItem('Update Sheet', 'updateSheet')
.addSeparator()
.addSubMenu(ui.createMenu('Test Menu')
.addItem('Test Function', 'menuItem2'))
.addToUi();
}
function updateSheet() {
/* Top Level Google Drive Folder ID & General Settings */
var TopDrivefolderId = "0B2rN5b8fW77ldXZXOXFLZGlSamc";
var BaseNumberOfFilesInFolder = 5;
var filesInFolderBeforeTemplate = 4;
/* End General Settings */
var parentFolder = DriveApp.getFolderById(TopDrivefolderId);
var childFolders = parentFolder.getFolders();
while(childFolders.hasNext()) {
var child = childFolders.next();
// Logger.log(child.getName() + " |Drive_ID: " + child.getId());
var folderId = child.getId();
// **the folderId Variable is also the folder ID,
// hence they are used interchangeably **
var myFolder = DriveApp.getFolderById(folderId);
/* Name of combined Spreadsheet*/
//var tradeName = Drive App.getFolderById(folderId).getName();
var tradeName = DriveApp.getFolderById(folderId).getName();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("application/vnd.google-apps.spreadsheet");
var spreadSheetName = myFolder.getName();
/* Check if a file needs to be created or if one already exists*/
var theFolder = DriveApp.getFolderById(folderId);
var files = theFolder.getFiles();
var fname = theFolder.getName();
var cnt = 0;
var file;
/* Check the quantity of files in a folder to see if you need to combine the sheets and create a file*/
while (files.hasNext()) {
cnt++;
file = files.next();
//Logger.log(file.getName());
if (cnt > BaseNumberOfFilesInFolder) {
Logger.log("File already exists")
break;
}
;
};
// If a file needs to be created this checks it and creates it
Logger.log(cnt + " is the number of files '"+ tradeName+ "' has.");
if (cnt > BaseNumberOfFilesInFolder){
Logger.log("The Data for " +tradeName+ " is already in the sheet.")
} else if(cnt == filesInFolderBeforeTemplate){
Logger.log("ERROR: You need to run the template adder script to add the template to " +tradeName)
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["ERROR: You need to run the template adder script to add the template to " +tradeName]);
continue;
}else{
/* Create the new spreadsheet to verify this trade is in the master sheet */
var newSpreadSheet = SpreadsheetApp.create("Added to Master");
var spreadSheet = DriveApp.getId(tradeName);
Logger.log(spreadSheet);
Logger.log(tradeName + " It has been logged");
var sheetCurrent = SpreadsheetApp.getActiveSheet();
//sheetCurrent.appendRow([tradeName,data]);
/* In order to move the file to the folder we want and because
google considers the SpreadSheet a Google Spreadsheet
instead of a file, we have to convert the SpreadSheet to a file in
order to move it.Thats what the next 2 lines of code do.*/
var getNewSSid = newSpreadSheet.getId();
var SStoGFile = DriveApp.getFileById(getNewSSid);
/* Actually moving the file*/
DriveApp.getFolderById(folderId).addFile(SStoGFile);
/* Deleting the duplicate file that was created in the process*/
var rootFolder = DriveApp.getRootFolder();
DriveApp.getRootFolder().removeFile(SStoGFile);
}
getSubFolders(child);
};
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('All Trades updated! Please check the sheet for errors.');
};
function getSubFolders(parent) {
parent = parent.getId();
var childFolder = DriveApp.getFolderById(parent).getFolders();
while(childFolder.hasNext()) {
var child = childFolder.next();
Logger.log(child.getName());
getSubFolders(child);
}
return;
}
/* End iterate through Sub Folders */
function menuItem2() {
var files = DriveApp.searchFiles("tradeName");
Logger.log(files,tradeName);
var spreadsheet = SpreadsheetApp.open(tradeName);
var sheet = spreadsheet.getSheets()[0];
Logger.log(sheet.getName());
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('You clicked the second menu item!');
};
。
你的代码有点太乱了,我无法真正找到你的问题所在以及你试图用它完成什么,但是根据你的问题本身,你需要做的是
fileList = DriveApp.getFilesByName('This_name')
while (fileList.hasNext()) {
Logger.log(fileList.next().getId())
}
这将记录与该名称匹配的每个文件 ID。电子表格服务只能通过 ID 打开,因此您需要使用驱动器服务(我的意思是 google-电子表格标签不适合这里,因为它只能作为驱动器处理)。
如果您想从电子表格服务中获取文件,您可以随时SpreadsheetApp.open(fileList.next())
直接从文件对象中打开它
我正在拔头发。我有以下代码,并且一直在尝试弄清楚如何使用文件名获取 gsheet 文件的 ID。我的目标是按名称查找文件,然后获取 ID,然后从该文件中获取一个范围并将其输出到活动 sheet
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Master Analysis Functions')
.addItem('Update Sheet', 'updateSheet')
.addSeparator()
.addSubMenu(ui.createMenu('Test Menu')
.addItem('Test Function', 'menuItem2'))
.addToUi();
}
function updateSheet() {
/* Top Level Google Drive Folder ID & General Settings */
var TopDrivefolderId = "0B2rN5b8fW77ldXZXOXFLZGlSamc";
var BaseNumberOfFilesInFolder = 5;
var filesInFolderBeforeTemplate = 4;
/* End General Settings */
var parentFolder = DriveApp.getFolderById(TopDrivefolderId);
var childFolders = parentFolder.getFolders();
while(childFolders.hasNext()) {
var child = childFolders.next();
// Logger.log(child.getName() + " |Drive_ID: " + child.getId());
var folderId = child.getId();
// **the folderId Variable is also the folder ID,
// hence they are used interchangeably **
var myFolder = DriveApp.getFolderById(folderId);
/* Name of combined Spreadsheet*/
//var tradeName = Drive App.getFolderById(folderId).getName();
var tradeName = DriveApp.getFolderById(folderId).getName();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("application/vnd.google-apps.spreadsheet");
var spreadSheetName = myFolder.getName();
/* Check if a file needs to be created or if one already exists*/
var theFolder = DriveApp.getFolderById(folderId);
var files = theFolder.getFiles();
var fname = theFolder.getName();
var cnt = 0;
var file;
/* Check the quantity of files in a folder to see if you need to combine the sheets and create a file*/
while (files.hasNext()) {
cnt++;
file = files.next();
//Logger.log(file.getName());
if (cnt > BaseNumberOfFilesInFolder) {
Logger.log("File already exists")
break;
}
;
};
// If a file needs to be created this checks it and creates it
Logger.log(cnt + " is the number of files '"+ tradeName+ "' has.");
if (cnt > BaseNumberOfFilesInFolder){
Logger.log("The Data for " +tradeName+ " is already in the sheet.")
} else if(cnt == filesInFolderBeforeTemplate){
Logger.log("ERROR: You need to run the template adder script to add the template to " +tradeName)
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["ERROR: You need to run the template adder script to add the template to " +tradeName]);
continue;
}else{
/* Create the new spreadsheet to verify this trade is in the master sheet */
var newSpreadSheet = SpreadsheetApp.create("Added to Master");
var spreadSheet = DriveApp.getId(tradeName);
Logger.log(spreadSheet);
Logger.log(tradeName + " It has been logged");
var sheetCurrent = SpreadsheetApp.getActiveSheet();
//sheetCurrent.appendRow([tradeName,data]);
/* In order to move the file to the folder we want and because
google considers the SpreadSheet a Google Spreadsheet
instead of a file, we have to convert the SpreadSheet to a file in
order to move it.Thats what the next 2 lines of code do.*/
var getNewSSid = newSpreadSheet.getId();
var SStoGFile = DriveApp.getFileById(getNewSSid);
/* Actually moving the file*/
DriveApp.getFolderById(folderId).addFile(SStoGFile);
/* Deleting the duplicate file that was created in the process*/
var rootFolder = DriveApp.getRootFolder();
DriveApp.getRootFolder().removeFile(SStoGFile);
}
getSubFolders(child);
};
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('All Trades updated! Please check the sheet for errors.');
};
function getSubFolders(parent) {
parent = parent.getId();
var childFolder = DriveApp.getFolderById(parent).getFolders();
while(childFolder.hasNext()) {
var child = childFolder.next();
Logger.log(child.getName());
getSubFolders(child);
}
return;
}
/* End iterate through Sub Folders */
function menuItem2() {
var files = DriveApp.searchFiles("tradeName");
Logger.log(files,tradeName);
var spreadsheet = SpreadsheetApp.open(tradeName);
var sheet = spreadsheet.getSheets()[0];
Logger.log(sheet.getName());
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('You clicked the second menu item!');
};
。
你的代码有点太乱了,我无法真正找到你的问题所在以及你试图用它完成什么,但是根据你的问题本身,你需要做的是
fileList = DriveApp.getFilesByName('This_name')
while (fileList.hasNext()) {
Logger.log(fileList.next().getId())
}
这将记录与该名称匹配的每个文件 ID。电子表格服务只能通过 ID 打开,因此您需要使用驱动器服务(我的意思是 google-电子表格标签不适合这里,因为它只能作为驱动器处理)。
如果您想从电子表格服务中获取文件,您可以随时SpreadsheetApp.open(fileList.next())
直接从文件对象中打开它