简体   繁体   English

如何在Google Apps脚本中使用Gsheet文件的名称获取ID

[英]How to I get the ID of a Gsheet file by using its name in google apps script

I am pulling my hair out. 我正在拔头发。 I have the following code and am stuck trying to figure out how to get the id of a gsheet file using its name. 我有以下代码,并且一直试图找出如何使用其名称获取gsheet文件的ID。 My goal is to look up a file by its name, then get the id, then grab a range from that file and output it into the active sheet 我的目标是按文件名查找文件,然后获取ID,然后从该文件中获取一个范围并将其输出到活动工作表中

 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!'); }; 

.

Your code is a bit too messy for me to actually find where exactly your issue is and what you try to accomplish with it, however based on your question itself, what you need to do is 对于我来说,您的代码有些混乱,无法真正找到您的问题的确切位置以及您要尝试解决的问题,但是根据您自己的问题,您需要做的是

fileList = DriveApp.getFilesByName('This_name')
while (fileList.hasNext()) {
  Logger.log(fileList.next().getId())
}

This will log each file ID that matches that name. 这将记录与该名称匹配的每个文件ID。 The spreadsheet service can only open by ID, so you need to use the drive service (what I mean is that the google-spreadsheet tags do not really fit here as this should only be handled as a drive thing). 电子表格服务只能通过ID打开,因此您需要使用驱动器服务(我的意思是google-spreadsheet标记在此处并不适合,因为这只能作为驱动器处理)。

If you want to get the file fro the spreadsheet service you can always do SpreadsheetApp.open(fileList.next()) to directly open it from the file object 如果要通过电子表格服务获取文件,则始终可以执行SpreadsheetApp.open(fileList.next())以直接从文件对象中打开文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 Gsheet 的 html 文件中的 Google Apps 脚本动态项目符号列表 - Google Apps Script dynamically bulleted list in an html file from Gsheet 来自 Gsheet 的 html 文件中的 Google Apps 脚本动态表格行 - Google Apps Script dynamically table row in an html file from Gsheet 在Google Apps脚本Web应用中使用Google Visualization API显示GSheet范围时出错 - Error showing GSheet range using Google Visualization API in Google Apps Script Web App 如何在Google Apps脚本中获取“this”文件的当前实例? - How do I get the current instance of “this” file in Google Apps Script? Google Apps脚本–从文件夹ID获取文件夹名称? - Google Apps Script – Get folder name from folder ID? Google Spreadsheet:如何使用Google Apps脚本命名范围 - Google Spreadsheet: How to name a range using Google Apps Script 如何从 Google Apps 脚本中的 XML 解析中获取元素 ID - How can I get the Element Id from XML parse in Google Apps Script 如何在 Google Apps 脚本中通过 id 获取特定工作表? - How to get specific sheet by id in Google Apps script? 如何在谷歌应用脚本中部分更改文件名? - How to partially change a file name in google apps script? 如何使用 Google Apps 脚本根据名称标准删除工作表? - How to delete sheets based on a name criteria using Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM