1

I am trying to create a google spreadsheet with an extra menu. This menu would trigger an action which would create a customized string of the content. This string needs to be written to an existing textfile stored already on google drive.

While searching for the possible solutions, I investigated the drive app, the file class and the blob class, but I could not figure out how to do the work.

I was able to retrieve the existing file (which is plain text btw) and show its content:

    var file = DriveApp.getFileById("EXISTING_FILE_ID");
    var fileContent = file.getBlob().getDataAsString();

But I cannot find out how to overwrite the existing file. It is not an option to create a new file, I need to stick to a specific ID. Both the spreadsheet and the written file will be under my ownership in my drive, shared publicly. Other users will modify the spreadsheet and so the file needs to be updated.

Fallback plan: If there is an alternative to reach this file by some path like on dropbox (and not by id) then it is OK to delete and recreate the file. How is this access possible?

4

2 回答 2

2

你有没有尝试过?

var file = DriveApp.getFileById("EXISTING_FILE_ID");
var fileContent = file.getBlob().getDataAsString();   
DocsList.getFileById("FILE_YOU_WANT_TO_REPLACE").replace(fileContent);

如果我的答案对您有用,请不要忘记单击我的答案旁边的绿色复选按钮。

于 2013-10-27T03:24:46.830 回答
-1

新方法是:

DriveApp.getFileByID("fileId").setContent(content)
于 2017-03-28T05:44:03.753 回答