在我們的組織中,我們都有 Google 的聯系方式。通常我們必須制作作業組串列,并且必須手動輸入我們的姓名和聯系方式。我想讓這個程序更有效率,但只找到了將我的郵件添加到此作業表的代碼。有什么辦法可以添加我的姓名和電話號碼嗎?
// 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.
}
uj5u.com熱心網友回復:
該getActiveUser方法只能回傳emailAddress. 但是,您可以輕松地使用People API高級服務來檢索您正在尋找的資訊:
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));
}
不要忘記通過轉到Services在 Apps Script 專案中添加高級服務:

參考
- 先進的人員服務。
uj5u.com熱心網友回復:
我剛剛用 anyfilenotepad 創建了這個檔案。您可以在 G Drive 的新按鈕中找到它。如果它不在那里,那么去更多并連接新的應用程式。創建獲取 id。把它放在下面的代碼中
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.');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/334410.html
標籤:谷歌应用程序脚本 谷歌表格 google-contacts-api
上一篇:復制谷歌驅動器檔案夾
