function createADocument () {
// Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create ('Hello, world!');
// Access the body of the document, then add a paragraph.
doc.getBody (). appendParagraph ('This document was created by Google Apps Script.');
}
Before running the code, you must save this script by clicking File> Save .
Rename the project, then click OK .
To run the code, click on the triangle icon on the toolbar.
You will have to grant the script permission to access your Google account through the pop-up window after clicking Run for the first time. Click Review Permissions to see what permissions the script needs.
Since this is not an application verified by Google, you will receive another warning. Basically it says, you should only proceed if you trust the developer. Click Advanced , then click Go to CreateNewDoc (or whatever name you have set for this script).
Review the permissions that the script requires, then click Allow .
Now, go to your Drive and if everything is right, you will see the file Hello, World! Here, double click on it to open.
When opening the file, you will see a line of text from the code added to your document.
Now, if you want to receive email notifications when the document is created, you can add a few lines of code to automatically send to your Google account. Add the following lines of code after doc.getBody (). AppendParagraph ('This document was created by Google Apps Script.'); but before the last curly brace:
// Get the URL of the document.
var url = doc.getUrl ();
// Get the address email of the active user - that's you.
var email = Session.getActiveUser (). getEmail ();
// Get the name of the document để sử dụng như là dòng đầu thư.
var subject = doc.getName ();
// Append a new string to the variable "url" to use as an email body.
var body = 'Link to your doc:' + url;
// Send yourself an email with a link to tài liệu.
GmailApp.sendEmail (email, subject, body);
Click the triangle icon. Since you have added a few additional lines that require additional permissions, you should follow the same steps, click Review Permissions > Advanced , then click Go to CreateNewDoc .
Note : Because Google warns you about launching an unverified application, you'll receive a security warning email. Google does this in case you suspect that you are not an authority for an unverified application.
Preview the permissions granted to the script, then click Allow . Once the document is created, you will receive an email with the link file in Google Drive.
Click on this link to directly open the file inside Google Drive.
Now we will create a limited script for Google Sheets to analyze the current worksheet that has the same item in a row and then delete it.
As mentioned, Bound script acts as an add-on for a specific file. To create a limited script, open the available Google Sheets sheet containing at least one duplicate data.
Click on Tool and then Script Editor .
Google Apps Script opens in a new tab with a blank script. However this time the script is limited in the form of Sheet. Just like before, delete the blank row and paste the following code:
// Removes duplicate rows from the current sheet.
function removeDuplicates () {
// Get current active Spreadsheet
var sheet = SpreadsheetApp.getActiveSheet ();
// Get all values from spreadsheet's hàng
var data = sheet.getDataRange (). getValues ();
// Create an array for non-duplicates
var newData = [];
// Iterate qua một đường dẫn của hàng
cho (var i trong dữ liệu) {
var row = data [i];
var duplicate = false;
cho (var j trong newData) {
if (row.join () == newData [j] .join ()) {
duplicate = true;
}
}
// If not a duplicate, put in newData array
if (! duplicate) {
newData.push (row);
}
}
// Delete the Sheet old and insert array này
sheet.clearContents ();
sheet.getRange (1, 1, newData.length, newData [0] .length) .setValues (newData);
}
Note : For scripts that delete duplicate data, all cells in the row must match.
Save and rename the script, then press the Run icon. Same as above, you must preview the required script permissions and grant it access to the sheet. Click Review Permissions . Then accept the message and click Allow to grant the script permission.
After running, go back to the Sheet and you will see the duplicate items disappear.
However, if the data inside a table is like the above example, this script will not change the size of the table to fit the number in it.
The above are two simple examples of how to use Apps Script, but it has many other options depending on your intended use.
I wish you all success!