英文:
Renaming a GAS project in a copied spreadsheet using Drive API and Google Apps Script
问题
以下是要翻译的内容:
"I've got a script that copies a spreadsheet templeate that's got a GAS project bounded to it. What I'm trying to do is change the name of the GAS project in the copied file. I've already reviewed this anwser: Rename appsscript project on duplication of spreadsheet but it doesen't seem to work for me.
I get the following error:
Error GoogleJsonResponseException: API call to drive.files.update failed with error: File not found: 1TKXKihWDHmPzasxFZH_-6OHDezdEFgiSfuQCcETJ3XYWBCO2kKNaa
So it seems that it's unable to find the project, maybe it has to do with all the files being in a Shared Drive?
Here is the code:
function changeNameScript_returnsID(name_File, file, scriptID, sheetID ,year, name_Building, YearFolder) {
//renames the script, copies the file in the desired folder and returns the id of the new file
var originalGASProjectName = "Script Template" + name_Building; // Please set the original project name of container-bound script ID of the template Spreadsheet.
var newGASProjectName = "Script " + name_Building+ " " + year; // Please set the new GAS project name.
// Rename to new project name.
Drive.Files.update({title: newGASProjectName},scriptID);
//makes a copy of the template in the desired folder, renames it and saves the ID of the file
var id_newFile = file.makeCopy(name_File, YearFolder).getId();
// Rename to original project name.
Drive.Files.update({title: originalGASProjectName}, scriptID);
return id_newFile
}
The error ocurs in:
Drive.Files.update({title: newGASProjectName}, scriptID);
Attempt at using supportsAllDrives: true
function canviarNomScript_retornaID(name_File, file, scriptID,year, name_Building, yearFolder) {
//de StackOverflow modificada, canvia el nom del Script, copia l'arxiu on toca i retorna la ID de l'arxiu
//link: https://stackoverflow.com/questions/60409396/rename-appsscript-project-on-duplication-of-spreadsheet
var originalGASProjectName = "Script Plantilla " + name_Building; // Please set the original project name of container-bound script ID of the template Spreadsheet.
var newGASProjectName = "Script " + name_Building + " " + year; // Please set the new GAS project name.
// Rename to new project name.
Drive.Files.update({title: newGASProjectName},scriptID,{supportsAllDrives: true });
//Copies the file and gets the ID
var id_newFile = file.makeCopy(name_File, yearFolder).getId();
// Rename to original project name.
Drive.Files.update({title: originalGASProjectName},scriptID,{supportsAllDrives: true });
return id_newFile
Error: Exception: The mediaData parameter only supports Blob types for upload.
Thanks in advance!"
英文:
I've got a script that copies a spreadsheet templeate that's got a GAS project bounded to it.
What I'm trying to do is change the name of the GAS project in the copied file.
I've already reviewed this anwser: Rename appsscript project on duplication of spreadsheet but it doesen't seem to work for me.
I get the following error:
Error GoogleJsonResponseException: API call to drive.files.update failed with error: File not found: 1TKXKihWDHmPzasxFZH_-6OHDezdEFgiSfuQCcETJ3XYWBCO2kKNaa
So it seems that it's unable to find the project, maybe it has to do with all the files being in a Shared Drive?
Here is the code:
function changeNameScript_returnsID(name_File, file, scriptID, sheetID ,year, name_Building, YearFolder) {
//renames the script, copies the file in the desired folder and returns the id of the new file
var originalGASProjectName = "Script Template" + name_Building; // Please set the original project name of container-bound script ID of the template Spreadsheet.
var newGASProjectName = "Script " + name_Building+ " " + year; // Please set the new GAS project name.
// Rename to new project name.
Drive.Files.update({title: newGASProjectName},scriptID);
//makes a copy of the template in the desired folder, renames it and saves the ID of the file
var id_newFile = file.makeCopy(name_File, YearFolder).getId();
// Rename to original project name.
Drive.Files.update({title: originalGASProjectName}, scriptID);
return id_newFile
}
The error ocurs in:
Drive.Files.update({title: newGASProjectName}, scriptID);
Attempt at using supportsAllDrives: true
function canviarNomScript_retornaID(name_File, file, scriptID,year, name_Building, yearFolder) {
//de StackOverflow modificada, canvia el nom del Script, copia l'arxiu on toca i retorna la ID de l'arxiu
//link: https://stackoverflow.com/questions/60409396/rename-appsscript-project-on-duplication-of-spreadsheet
var originalGASProjectName = "Script Plantilla " + name_Building; // Please set the original project name of container-bound script ID of the template Spreadsheet.
var newGASProjectName = "Script " + name_Building + " " + year; // Please set the new GAS project name.
// Rename to new project name.
Drive.Files.update({title: newGASProjectName},scriptID,{supportsAllDrives: true });
//Copies the file and gets the ID
var id_newFile = file.makeCopy(name_File, yearFolder).getId();
// Rename to original project name.
Drive.Files.update({title: originalGASProjectName},scriptID,{supportsAllDrives: true });
return id_newFile
Error: Exception: The mediaData parameter only supports Blob types for upload.
Thanks in advance!
答案1
得分: 1
从:
Drive.Files.update({ title: newGASProjectName }, scriptID, { supportsAllDrives: true });
到:
Drive.Files.update({ title: newGASProjectName }, scriptID, null, { supportsAllDrives: true });
或者,在您的情况下,文件内容为null。因此,可以使用以下修改:
Drive.Files.patch({ title: newGASProjectName }, scriptID, { supportsAllDrives: true });
-
Drive.Files.update
的参数为update(resource: Drive_v2.Drive.V2.Schema.File, fileId: string, mediaData: Blob, optionalArgs: Object)
。 -
Drive.Files.patch
的参数为patch(resource: Drive_v2.Drive.V2.Schema.File, fileId: string, optionalArgs: Object)
。 -
在
supportsAllDrives
的情况下,请将其包括在查询参数中。在这种情况下,请将其包括在Drive API的高级Google服务的optionalArgs
中。
参考:
英文:
In your script, how about the following modification?
From:
Drive.Files.update({title: newGASProjectName},scriptID,{supportsAllDrives: true });
To:
Drive.Files.update({ title: newGASProjectName }, scriptID, null, { supportsAllDrives: true });
or, in your situation, the file content is null. So, the following modification might be able to be used.
Drive.Files.patch({ title: newGASProjectName }, scriptID, { supportsAllDrives: true });
-
The arguments of
Drive.Files.update
areupdate(resource: Drive_v2.Drive.V2.Schema.File, fileId: string, mediaData: Blob, optionalArgs: Object)
. -
The arguments of
Drive.Files.patch
arepatch(resource: Drive_v2.Drive.V2.Schema.File, fileId: string, optionalArgs: Object)
. -
In the case of
supportsAllDrives
, please include it in the query parameter. In this case, please include it asoptionalArgs
of Drive API at Advanced Google services.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论