Search code examples
google-apps-scriptgoogle-sheetsgoogle-api

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!


Solution

  • 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: