How to add a slide / image to a slide in Google Drive SDK javascript

285 views Asked by At

I've been struggling with this for a little while now and couldn't find anything about it, anyone have any suggestions?

Is there a good way to add a slide to a presentation in Drive via the Google drive sdk for javascript? A way to add an image to a presentation as a new slide would be awesome. I've tried the following, with a constant 500 error.

function updateFile(fileId, imgurl, callback) {
    const
    boundary = '--314159265358979323846';
    const
    delimiter = "\r\n--" + boundary + "\r\n";
    const
    close_delim = "\r\n--" + boundary + "--";

    var contentType = 'application/octet-stream';
    var metadata = {
        'title' : 'test pres'
    };
    console.log(imgurl);
    var base64Data = imgurl.replace("data:image/png;base64,", "");
    var multipartRequestBody = delimiter
            + 'Content-Type: application/json\r\n\r\n'
            + JSON.stringify(metadata) + delimiter + 'Content-Type: '
            + contentType + '\r\n' + 'Content-Transfer-Encoding: base64\r\n'
            + '\r\n' + base64Data + close_delim;

    var request = gapi.client.request({
        'path' : '/upload/drive/v2/files/' + fileId,
        'method' : 'PUT',
        'params' : {
            'uploadType' : 'multipart',
            'alt' : 'json'
        },
        'headers' : {
            'Content-Type' : 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body' : multipartRequestBody
    });
    if (!callback) {
        callback = function(file) {
            console.log(file)
        };
    }
    request.execute(callback);
}

I've confirmed that I can create a presentation and image file with other code as well, but I can't seem to find a way to add to a presentation.

Anyone have any idea if this is possible?

Thanks, Mike

1

There are 1 answers

0
pinoyyid On

The Drive SDK only supports whole-file operations. Anything which you want to do within a file is unsupported. For example, in the case of spreadsheets there is a separate spreadsheets API to manipulate rows and cells. For presentations, I'm not aware of an equivalent API.