In our organisation we all have our contact details in Google. Often we have to make working group lists and we have to type our name and contact details manually. I wanted to make this process more efficient but only found the code to add my mail to this sheet. Any way to add my name and phone number as well?
// The function below will add a custom menu item to the spreadsheet.
function onOpen() {
var ui = SpreadsheetApp.getUi(); // This line declares our first variable, the UI.
ui.createMenu('Add me to the WGL') // This line has an action which creates a Menu called 'Add me to the WGL'
.addItem('Add me', 'AddMe') // This line will create a sub-menu item called 'Add me'
.addToUi(); // This line adds the item created above to the UI
}
// The function below will take the variables declared below and plug them into our spreadsheet.
function AddMe() {
var me = Session.getActiveUser(); // This line allows us to declare who the active user is - the person running the function.
var actionedBy = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data").getRange('A2').setValue(me); // This line declares the cell we wish to plug our variable 'me' into.
SpreadsheetApp.getUi()
.alert('Thanks! Your details have been added to the WGL.'); // This line lets us push a popup message once the function is run.
}
CodePudding user response:
The getActiveUser
method can only return the emailAddress
. However, you can easily make use of the People API
advanced service in order to retrieve the information you are looking for:
function AddMe() {
var people = People.People.getBatchGet({
resourceNames: ['people/me'],
personFields: 'names,phoneNumbers'
});
var actionedBy = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data").getRange('A2').setValue('Myself: %s', JSON.stringify(people, null, 2));
}
Do not forget to add the advanced service in the Apps Script project by going to Services:
Reference
CodePudding user response:
I just created the file with anyfilenotepad. You can find it in your new button in G Drive. If it's not there then go to more and connect new apps. Create the get the id. Put it in the code below
function createInfoFile() {
const infofileid = "infofileid";
const file = DriveApp.getFileById(infofileid);
const name = Browser.inputBox("enter name");
const phone = Browser.inputBox("enter phone");
const obj = { name: name, phone: phone };
file.setContent(JSON.stringify(obj));
}
function AddMe() {
const me = Session.getActiveUser();
const obj = JSON.parse(DriveApp.getFileById("fileid").getBlob().getDataAsString());
const s = `${me}\n${obj.name}\n${obj.phone}`
const actionedBy = SpreadsheetApp.getActive().getSheetByName("Data").getRange('A2').setValue(s );
SpreadsheetApp.getUi().alert('Thanks! Your details have been added to the WGL.');
}