Script for a Google Doc .makeCopy() the comments aren't copied? - google-apps-script

Hello is possible twhen copying a Google Doc document to copy also the comments in the "copy doc."Because I've tried this with the TEMPLATE_DOC_ID which has many comments and I don't find the comments in the "copy".I am missing something?It's another method? Thanks!
//Make a copy of the template file
var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();

Unfortunately, the Google Docs copied by makeCopy() don't include the comments. So the comments and replies are required to be inserted to the copied file, after the file was copied. In order to implement this, please enable Drive API at Advanced Google Services and API console.
Enable Drive API v2 at Advanced Google Services
On script editor
Resources -> Advanced Google Services
Turn on Drive API v2
Enable Drive API at API console
About Drive API, in your environment, this might have already been enabled.
On script editor
Resources -> Cloud Platform project
View API console
At Getting started, click Enable APIs and get credentials like keys.
At left side, click Library.
At Search for APIs & services, input "Drive". And click Drive API.
Click Enable button.
If API has already been enabled, please don't turn off.
Sample script :
var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();
// Added script
var commentList = Drive.Comments.list(TEMPLATE_DOC_ID);
commentList.items.forEach(function(item) {
var replies = item.replies;
delete item.replies;
var commentId = Drive.Comments.insert(item, documentId).commentId;
replies.forEach(function(reply) {
Drive.Replies.insert(reply, documentId, commentId).replyId;
});
});
Note :
Unfortunately, the create time and modified time couldn't updated. So the date becomes the created date.
References :
Advanced Google Services
Drive API
Comments: insert
Replies: insert
If this was not what you want, I'm sorry.

Related

Google App Script Advanced Service: Drive API

https://spreadsheet.dev/automatically-convert-excel-spreadsheets-to-google-sheets-using-apps-script
On the page above, Drive API is introduced to convert excel file to Google Sheet
let blob = excelFile.getBlob();
let config = {
title: "[Google Sheets] " + excelFile.getName(),
parents: [{id: excelFile.getParents().next().getId()}],
mimeType: MimeType.GOOGLE_SHEETS
};
let spreadsheet = Drive.Files.insert(config, blob);
I've been checking Google Drive API's ducumentation and references, but couldn't find anything regarding Drive.Files and Drive.Files.insert. Can someone direct me to the right documentation so I can learn to use these interfaces myself? Thanks!
Unfortunately, it seems that the detailed document of Drive API of Advanced Google services is not been officially published. But, in your situation, I thought that the document at the autocomplete of the script editor of Google Apps Script might be useful.
When the script editor of Google Apps Script is used, you can use the autocomplete of each method. In this case, when Drive API is enabled at Advanced Google services, this autocomplete can be also used for the methods of Drive API. When this is used, the documents of each method can be seen as follows. As a sample, Drive.Files.insert is used.
This completion includes an explanation of the method. In the case of Drive.Files.insert, it is found that it is Insert a new file. and the arguments are
resource: Drive_v2.Drive.V2.Schema.File
This is a request body of "Files: insert" of Drive API v2 Ref
mediaData: Blob
This is a Blob.
optionalArgs: Object
This is the query parameter of "Files: insert" of Drive API v2 Ref
, respectively. From this, it is found that this is Drive API v2. And, Drive_v2.Drive.V2.Schema.File is returned. This can be seen at here.
I thought that this document of the autocomplete with the script editor might be useful for understanding how to use Drive API at Advanced Google services.
Note:
This autocomplete can be also used for all APIs (Sheets API, Docs API, and so on) of Advanced Google services. I think that the document shown with the autocomplete with the script editor might be useful for understanding how to use the APIs at Advanced Google services.
References:
Advanced Google services
Using autocomplete
You may look into the use of Drive.Files and Drive.Files.insert under Advanced Drive Service.
It is somewhat the same use of Drive API, but the provided code on the reference you have provided leans toward using Drive API on App Script (which has a somewhat different syntax as oppose to using Drive API with other languages)
References:
https://developers.google.com/apps-script/advanced/drive
https://developers.google.com/drive/api/v2/reference/files/insert
https://developers.google.com/drive/api/samples

Google Docs API see Docs without knowing id

I'm creating a Chrome extension which when highlighting text anywhere will offer the user to save the highlighted text to any Doc in Google Docs. So the idea is to show a list of last 10 Google Docs and the user could save it to any of them or could search for another Doc.
I've been reading documentation on Chrome identity API, Docs API and Drive API, and they all mention that we need to know the Doc's id (which is inside the url of the Doc).
So it's not possible to display a list of Docs without knowing their actual ids? I guess it would be a security violation if an extension or an app could do this? Or I'm mistaken and it's actually possible?
P.S. This link seems to be what I was looking for (so it's possible): https://developers.google.com/drive/api/v3/reference/files/list
Answer:
Yes, you can get a list of the last 10 modified files in Google Drive using the modifiedDate, pageSize and mimeType parameters.
More Information:
You can make a Files: list query to the Drive API with parameters narrowing down your search. As you do not know the file IDs, you can search for files which have the application/vnd.google-apps.document MIME Type, and then narrow the search down by ordering by the modifiedDate and only requesting 10 results.
Code:
As I'm not 100% sure on which language you are using, I've provided a simple Apps Script example below. The relevant references can be seen below though so you can modify this to suit your coding requirements.
function myFunction() {
var searchTerms = {
orderBy: "modifiedDate",
pageSize: 10,
q: "mimeType='application/vnd.google-apps.document'"
};
for (var i = 0; i < 10; i++) {
Logger.log(Drive.Files.list(searchTerms).items[i].title);
}
}
Don't forget to enable the Advanced Drive Service for this! Also remember that the Advanced Drive service uses Drive v2, though this can be done in both Drive API v2 and Drive API v3.
References:
Google Drive API v3 - Files: list
Advanced Drive Service - Listing folders
G Suite and Drive MIME Types

Where do I find the tasklist ID

I am trying to create tasks in a tasklist from the google app script editor. Looks like I need the id of the tasklist to be able to add the task.
Problem is I can figure out where ti find the tasklist ID
If you want to retrieve the tasklist IDs using Google Apps Script, how about this sample script?
In order to use the sample script, before you run the script, please enable Tasks API at Advanced Google Services and API console as follows.
Enable Tasks API at Advanced Google Services
On script editor
Resources -> Advanced Google Services
Turn on Tasks API v1
Enable Tasks API at API console
On script editor
Resources -> Cloud Platform project
View API console
At Getting started, click "Explore and enable APIs".
At left side, click Library.
At "Search for APIs & services", input "Tasks API". And click "Tasks API".
Click Enable button.
If API has already been enabled, please don't turn off.
Sample script:
After Tasks API was enabled, please run the following script.
function myFunction() {
var taskLists = Tasks.Tasklists.list().getItems();
var res = taskLists.map(function(list) {
return {
taskListId: list.getId(),
listName: list.getTitle(),
};
});
Logger.log(res)
}
Reference:
Google Tasks API
If this was not the result you want, I apologize.

Is there a way to add multiple labels at once to a Gmail thread in a google app script?

I am trying to add some labels to Gmail threads in a google-app script programmatically.
Using thread.addLabel function right now.
Because I am adding multiple labels to many threads, this approach is first slow and second inefficient in quota usage. I have to call thread.allLabel for every label for every thread.
Is there a way to add multiple labels to Gmail threads / messages together?
The Gmail API has a thread.modify function that can be used to add multiple labels. Is there something similar in google apps script ?
You want to add several labels to a thread using Google Apps Script. If my understanding is correct, how about using Gmail API of Advanced Google Services? In GmailApp, I couldn't find methods for adding several labels to a thread. So I propose this. I think that you can achieve it using gmail.users.messages.modify. The sample script is as follows. In order to use this script, please enable Gmail API at Advanced Google Services and API console.
Enable Gmail API v1 at Advanced Google Services
On script editor
Resources -> Advanced Google Services
Turn on Gmail API v1
Enable Gmail API at API console
On script editor
Resources -> Cloud Platform project
View API console
At Getting started, click Enable APIs and get credentials like keys.
At left side, click Library.
At Search for APIs & services, input "Gmail". And click Gmail API.
Click Enable button.
If API has already been enabled, please don't turn off.
If now you are opening the script editor with the script for using Gmail API, you can enable Gmail API for the project by accessing this URL https://console.cloud.google.com/apis/api/gmail.googleapis.com/overview
Flow of script :
Retrieve label list.
Retrieve label IDs using label names.
Add the labels to a thread.
Sample script :
var userId = "me";
var threadId = "### thread ID ###"; // Please input a thread ID.
var labels = ["### label name 1 ###", "### label name 2 ###"]; // Please input label names here.
var labelList = Gmail.Users.Labels.list(userId).labels;
var labelIds = labels.map(function(e){return labelList.filter(function(f){return f.name == e})[0].id});
Gmail.Users.Messages.modify({"addLabelIds": labelIds}, userId, threadId);
Note :
This is a simple sample script. So please modify it to your environment.
References :
Advanced Google Services
Gmail API
If I misunderstand your question, I'm sorry.

Google apps script drive file: How to get user who last modified file?

To get the date a file was last updated you can call getLastUpdated()
How to I get the user who did this last update in google apps script code?
I have tried to run the code under listing revisions at Advanced Drive Service
var revisions = Drive.Revisions.list(fileId);
but I get
ReferenceError: "Drive" is not defined.
https://developers.google.com/apps-script/guides/services/advanced
In Resouces > Advanced Google services, turn on Drive API...
In that same window > click Google Developer's Console >> enable both Drive API and Drive SDK...
then...
Drive.Files.get('<FILEDID>').lastModifyingUser
To make it clear:
First enable the drive API and SDK as described by Bryan P. Then you can use
var lastemailuser = Drive.Files.get(<FILEDID>).lastModifyingUser.emailAddress;
var lastnameuser = Drive.Files.get(<FILEDID>).lastModifyingUserName;