1

I have a spreadsheet with prepared template and a bunch of Apps Script code to work with sheets. I need to export data into this template using Google Sheets API, but doing it in the same spreadsheet for all users is not the best idea. Therefore, I need to create a new spreadsheet for each user. I managed to create an empty spreadsheet, copied template sheet from original spreadsheet and inserted it in a new one like this:

//Creating new Spreadsheet
$service = new Google_Service_Sheets($client);
$serviceDrive = new Google_Service_Drive($client);

$spreadsheet = new Google_Service_Sheets_Spreadsheet([
    'properties' => [
        'title' => Lang::get('pls.export.spreadsheet_name'),
    ]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
    'fields' => 'spreadsheetId'
]);

//Copying sheet from template
$sourceSpreadsheet = 'spreadsheet id goes here';
$soureSheet = 'sheet id goes here';
$requestBody = new Google_Service_Sheets_CopySheetToAnotherSpreadsheetRequest(['destinationSpreadsheetId' => $spreadsheet->spreadsheetId]);
$response = $service->spreadsheets_sheets->copyTo($sourceSpreadsheet, $soureSheet, $requestBody);

This works and I can also write data in it, but the problem is, I can't copy the Apps Script code. I have been also looking for a way to somehow duplicate the whole old Spreadsheet and import the data there, but could not find anything (Not sure if it helps to copy the Apps Script codes though). Is it possible to copy the Apps Script codes using Google Sheets API and insert it in new Spreadsheet? Or how do I duplicate the whole spreadsheet?

6
  • Use Drive api? Note that copying code doesn't mean it's the same project-it's a copy and needs to be authorized again. Commented Sep 22, 2020 at 12:16
  • Is the Apps Script project a standalone or container bound project? Commented Sep 22, 2020 at 12:52
  • @TheMaster Yes, I need to make it a separate Spreadsheet. Then I share it to a logged in user, so they can view and edit things as they want Commented Sep 22, 2020 at 12:52
  • @Aerials The template is a standard google spreadsheet. App script is added manually Commented Sep 22, 2020 at 12:53
  • You want to create a new sheet for each of the users? And will these sheets live in your Drive? Why not for example use the template spreadsheet and create a new internal sheet for each user's data? Commented Sep 22, 2020 at 13:01

1 Answer 1

1

I solved this issue by using Google Drive API and copied the file from there instead:

$serviceDrive = new Google_Service_Drive($client);
$drive = new Google_Service_Drive_DriveFile();
$spreadsheet = $serviceDrive->files->copy('your spreadsheet id', $drive);

Then shared the created file to user like this:

$this->insertPermission($serviceDrive, $spreadsheet->id, $user->email, 'user', 'writer');

Works like a charm!

EDIT: Here is the insertPermission function

function insertPermission($service, $fileId, $value, $type, $role)
{
    $newPermission = new Google_Service_Drive_Permission();
    $newPermission->setEmailAddress($value);
    $newPermission->setType($type);
    $newPermission->setRole($role);
    try {
        return $service->permissions->create($fileId, $newPermission);
    } catch (Exception $e) {
        print $e->getMessage();
    }
    return NULL;
}
Sign up to request clarification or add additional context in comments.

5 Comments

OP specifically asked for Sheets API
@Datadimension I'm the OP, I know what I asked for and I answered my own question :)
My apologies, if you can slightly edit your answer, I can happily remove the downvote as its locked by Stackoverflow
@Datadimension Hmm, downvote and upvote should work fine even when the answer is marked correct
It complained on my phone but seems to have accepted on PC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.