Reputation: 581
Let's say I have this minimal XML file on Google Drive.
<?xml version="1.0" encoding="UTF-8"?>
<MyCounter>
<counter>137</counter>
</MyCounter>
Using Google Script, I want to:
I'm at step 2 at the moment. I can delete the old file and create a new one with the same name and updated content. I prefer to update the existing one instead, so it will maintain the unique ID, and I can access the file with said ID instead of searching for it via file name.
Upvotes: 1
Views: 601
Reputation: 201513
137
of <counter>137</counter>
in the file.If my understanding is correct, how about this sample script? I think that there are several answers for your situation. So please think of this as just one of them.
number
of <counter>{number}</counter>
using replace()
.setContent()
.By this flow, the file can be updated without changing file ID.
var fileId = "#####"; // Please set fileId here.
var file = DriveApp.getFileById(fileId);
var str = file.getBlob().getDataAsString();
var r = str.replace(/<counter>(\d+)<\/counter>/, function(_, p) {
return "<counter>" + (Number(p) + 1) + "</counter>";
});
file.setContent(r);
Upvotes: 1