Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

google apps script - How to read a txt. file, copy the content into a new file and replace certain content

I have a .txt file online filled with text such as:

"Type": "Internal",
"Category": 1,
"Presentation": 3,
...

I want to use Apps Script to read out the content of that file and ultimately save it in a new file with modified content. Specifically, I need to remove the underscore of some content but keep the rest intact.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Apps Script uses the JavaScript programming language. For Apps Script code that runs on Google's servers, JavaScript code will be in a .gs "script" file. JavaScript was originally created for "client side" use, which means it runs in the browser that you have open on your computer (Not Google's server). In client side JavaScript, the code is put into HTML <script> tags. But Apps Script also uses JavaScript for server side code, it's used for both.

If the text file is in your Google Drive, you can access it in various ways. One way is to use Drive Service

Here is some sample code that can get a file from Google Drive:

var allFilesInFolder,cntFiles,docContent,fileNameToGet,fldr,
    thisFile,whatFldrIdToUse;//Declare all variable at once

whatFldrIdToUse = '123ABC_Put_Your_Folder_ID_here';
fileNameToGet = 'myText.txt';//Assign the name of the file to get to a variable

//Get a reference to the folder    
fldr = DriveApp.getFolderById(whatFldrIdToUse);

//Get all files by that name. Put return into a variable
allFilesInFolder = fldr.getFilesByName(fileNameToGet);
Logger.log('allFilesInFolder: ' + allFilesInFolder);

if (allFilesInFolder.hasNext() === false) {
  //If no file is found, the user gave a non-existent file name
  return false;
};

cntFiles = 0;
//Even if it's only one file, must iterate a while loop in order to access the file.
//Google drive will allow multiple files of the same name.
while (allFilesInFolder.hasNext()) {
  thisFile = allFilesInFolder.next();
  cntFiles = cntFiles + 1;
  Logger.log('File Count: ' + cntFiles);

  docContent = thisFile.getAs('text/plain');
  Logger.log('docContent : ' + docContent );
};

To see values generated by the code, go into the Logs dialog box. Click the "View" menu, and then choose "Logs" from the menu. The Logger.log() statements print content to the log.

The content you gave for an example looks like JSON. You can convert a string that is in JSON format to an object with JSON.parse(). From there you can add or change values. If you want to convert the JSON back to a string before putting it back into a text file, you can use JSON.stringify()

JSON reference Mozilla

In order to find specific characters in a string, you'll need to learn JavaScript string functions.

To replace all occurrences of a certain piece of text, you can use replace:

Information about replace()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...