问题描述
如何获取 URL 中的电子表格 ID.我知道使用 SpreadsheetApp.getActiveSpreadsheet().getId() 和 SpreadsheetApp.getActiveSpreadsheet().getUrl()
How to get the spreadsheet ID that is in the URL.I know about using SpreadsheetApp.getActiveSpreadsheet().getId() and SpreadsheetApp.getActiveSpreadsheet().getUrl()
但是 getId()/getUrl()/getKey() 现在会因 Google-Drive-SDK 而失败.我不再能够使用此 ID 通过 Drive-SDK 打开文件.它大约在 1 周前工作,但 Drive-SDK 现在无法打开文件.
But getId()/getUrl()/getKey() now fails with Google-Drive-SDK. I am no longer able touse this ID to open files with the Drive-SDK. It was working about 1 week ago, but the Drive-SDK now fails to open the file.
一种解决方法是使用 URL 中的 ID.key=之后的id=
A workaround is to use the ID that is in the URL. The id after key=
https://docs.google.com/spreadsheet/ccc?key=0AkGlO9jJLGO8dHJrSE0wTEF2VXRSdGRlNVQaaVRad0E
但我想要一种自动化的方式来获取它,而不是手动过程.由于电子表格是使用模板创建的,所以我无法知道 ID.想一想,有没有办法得到它?
But I want an automated way to get it, nota manual process. As the spreadsheet is being created using a template, so I don't have a way to know the ID. Thoughts, is there a way to get it?
注意 1:stackoverflow.com/questions/18583885/说 Google Drive SDK 问题应该发布到 stackoverflow
Note 1: stackoverflow.com/questions/18583885/ says Google Drive SDK issues should be posted to stackoverflow
注意 2:getUrl() 和 getKey() 包含与 getId() 相同的 ID,而不是 URL 中的键.所以看起来对密钥的访问权限已被删除,所以我不抱太大希望.
Note 2: getUrl() and getKey() contains the same ID as getId() not the key in the URL. So it looks like access to the key has been removed, so am not holding out much hope.
驱动代码是 Java 而不是 google-apps-script.版本 1.15.0-rc
The drive code, is Java rather than google-apps-script. version 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();
我认为它在上周左右的某个时候坏了.它因执行()而失败,它会产生 404 错误.当我使用由谷歌应用程序脚本生成的短电子表格密钥时,它会给出错误.但是当我使用浏览器 URL 键值时它仍然可以正常工作.
I think it broke sometime in the last week or so. It fails with the execute(), it produces a 404 error. It gives the error when I use with the short spreadsheetKey that is produced by google apps script. But it still works fine when I use the browser URL key value.
推荐答案
你没有显示不适合你的代码,但是下面的代码对我来说没问题
You don't show the code that is not working for you, but the following works ok for me
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())
}
这篇关于获取 URL 中的电子表格密钥.不是 ss.getId()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!