使用Drive API和Google Apps Script在复制的电子表格中重命名GAS项目。

huangapple go评论68阅读模式
英文:

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 are update(resource: Drive_v2.Drive.V2.Schema.File, fileId: string, mediaData: Blob, optionalArgs: Object).

  • The arguments of Drive.Files.patch are patch(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 as optionalArgs of Drive API at Advanced Google services.

References:

huangapple
  • 本文由 发表于 2023年5月29日 17:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定