Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 获取URL中的电子表格键。不是ss.getId()_Google Apps Script_Google Drive Api - Fatal编程技术网

Google apps script 获取URL中的电子表格键。不是ss.getId()

Google apps script 获取URL中的电子表格键。不是ss.getId(),google-apps-script,google-drive-api,Google Apps Script,Google Drive Api,如何获取URL中的电子表格ID。 我知道如何使用SpreadsheetApp.getActiveSpreadsheet().getId()和SpreadsheetApp.getActiveSpreadsheet().getUrl() 但是getId()/getUrl()/getKey()现在无法使用Google Drive SDK。我再也做不到了 使用此ID可使用驱动器SDK打开文件。大约一周前,它还在工作,但驱动器SDK现在无法打开该文件 解决方法是使用URL中的ID。键后的id= 但我想要

如何获取URL中的电子表格ID。 我知道如何使用SpreadsheetApp.getActiveSpreadsheet().getId()和SpreadsheetApp.getActiveSpreadsheet().getUrl()

但是getId()/getUrl()/getKey()现在无法使用Google Drive SDK。我再也做不到了 使用此ID可使用驱动器SDK打开文件。大约一周前,它还在工作,但驱动器SDK现在无法打开该文件

解决方法是使用URL中的ID。键后的id=

但我想要一种自动的方式来获取它,而不是 人工过程。由于电子表格是使用模板创建的,所以我没有办法知道ID。想一想,有没有办法得到它

注1:stackoverflow.com/questions/18583885/表示谷歌硬盘SDK问题应发布到stackoverflow

注意2:getUrl()和getKey()包含与getId()相同的ID,而不是URL中的键。看来钥匙已经被取下了,所以我不抱太大希望

编辑 驱动代码是Java,而不是google应用程序脚本。版本1.15.0-rc

    Drive driveService = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("CellMaster.com.au").build();
    File file = driveService.files().get(this.spreadsheetKey).execute();

我想它在上个星期左右的某个时候坏了。它使用execute()失败,产生404错误。当我使用google apps脚本生成的短电子表格键时,它给出了一个错误。但是,当我使用浏览器URL键值时,它仍然可以正常工作。

您不会显示对您不起作用的代码,但以下代码对我来说可以

function myFunction() {
// create a new spreadsheet workbook
var ss = SpreadsheetApp.create("Test");
var id = ss.getId();
Logger.log(id)
// get the ss file through Drive using spreadsheet id
var file = DriveApp.getFileById(id);
var driveid = file.getId();
Logger.log(driveid)
// open the spreadsheet using id of file
var ssfromdrive = SpreadsheetApp.openById(driveid);
Logger.log(ssfromdrive.getName())
 }

您不会显示对您不起作用的代码,但是下面的代码对我来说可以

function myFunction() {
// create a new spreadsheet workbook
var ss = SpreadsheetApp.create("Test");
var id = ss.getId();
Logger.log(id)
// get the ss file through Drive using spreadsheet id
var file = DriveApp.getFileById(id);
var driveid = file.getId();
Logger.log(driveid)
// open the spreadsheet using id of file
var ssfromdrive = SpreadsheetApp.openById(driveid);
Logger.log(ssfromdrive.getName())
 }

正如David指出的,同一个文件的id在电子表格应用程序和驱动器应用程序中是不同的


使用电子表格id从驱动器应用程序获取电子表格文件,然后按照下面的示例获取驱动器文件的id将解决您的问题。

正如David指出的,电子表格应用程序和驱动器应用程序中同一文件的id不同

使用电子表格id从驱动器应用程序中获取电子表格文件,然后按照下面的示例获取驱动器文件的id,将解决您的问题。

是的,它可以工作。“var driveid=file.getId();”给出URL中的长电子表格键。谢谢为了记录,我在问题中添加了失败的代码。他说“Java”,并发布了JavascriptYes,它是有效的。“var driveid=file.getId();”给出URL中的长电子表格键。谢谢为了证明这一点,我在问题中添加了失败的代码。他说“Java”,并发布了Javascript