【问题标题】:Exception: The parameters don't match the method signature for ContactsApp.createContact异常:参数与 ContactsApp.createContact 的方法签名不匹配
【发布时间】:2021-06-21 15:58:10
【问题描述】:

我想创建一个自动化流程,将 Google 表单电子表格客户数据转换为 Google 联系人。我不确定它指的是哪个字符串与 ContactsApp.createContact 的签名不匹配。

我收到一条错误消息:

异常:参数 (String,String,String,String) 与 ContactsApp.createContact 的方法签名不匹配。 addContact@Google Contacts.gs:114

当我尝试在表单完成后实现触发器以自动将联系人添加到 Google 联系人中时,它只显示名字、姓氏、电子邮件,但不显示电话。。 p>

有错误的代码部分

function addContact()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var cell = sheet.getActiveCell();
    var active_row = cell.getRow();
    var range = sheet.getDataRange();
  
    var first_name = range.getCell(active_row ,1).getValue();
    var last_name = range.getCell(active_row ,2).getValue();
    var email = range.getCell(active_row,3).getValue();
    var phone = range.getCell(active_row,4).getValue();  
    
    var contact = ContactsApp.createContact(first_name, last_name, email, phone);   (LINE 114)
    
    var mainGroup = ContactsApp.getContactGroup("System Group: My Contacts");
    
    mainGroup.addContact(contact);
 
  
    showOutputBox('first name' + first_name + '\nlast name' + last_name + '\nemail' + email + '\nphone' + phone, "added contact");
  
}

这是完整的代码

// Add a menu
// Adds a menu item Contacts → add Contact
// Add a handler to handle when you click on that menu item.

function onOpen() 
{
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Contacts')
      .addItem('add Contact', 'addContact')
      .addToUi();
}

function formSubmitted(e){

  addContact(e.namedValues);
}



// Add a contact from the Google sheet
// Picks up the contact from the currently selected cell.
// Google Sheet, first column First name, second column last name and third column email.
// Pick the values and call ContactsApp.createContact()


function addContact()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var cell = sheet.getActiveCell();
    var active_row = cell.getRow();
    var range = sheet.getDataRange();
  
    var first_name = range.getCell(active_row ,1).getValue();
    var last_name = range.getCell(active_row ,2).getValue();
    var email = range.getCell(active_row,3).getValue();
    var phone = range.getCell(active_row,4).getValue();  
    
    var contact = ContactsApp.createContact(first_name, last_name, email, phone);
    
    
    showOutputBox('first name' + first_name + '\nlast name' + last_name + '\nemail' + email + '\nphone' + phone, "added contact");
  
}

function showOutputBox(str, title)
{
  var html = HtmlService.createHtmlOutput('<pre>'+str+'</pre>')
    .setWidth(400)
    .setHeight(300);
  
    SpreadsheetApp.getUi() 
    .showModalDialog(html, title);
}



// Where is the new contact?
// If you open contacts.google.com you still will not see the new contact yet. There was no error too. So where did the new contact disappear?
// The reason is that the new contact is not a member of any of your contact groups. There are predefined groups called “system groups” in every account.
// Let us first add a function to list all the groups.

function showContactGroups() 
{
  var groups = ContactsApp.getContactGroups();
  var str ='Groups\n';
  for(var g = 0; g < groups.length; g++) 
  {
    str +='\n'+groups[g].getName()
    
  }
  
  showOutputBox(str,'Your Contact Groups');
}




// Add a menu item to show the contact groups:
// Now your Contacts menu should have tow items. Select Contacts → show Groups.
// Show all the contact groups in your account

function onOpen() 
{
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Contacts')
      .addItem('show Groups', 'showContactGroups')
      .addItem('add Contact', 'addContact')
      .addToUi();
}



// Let us add your new contacts to “My Contacts” group.
// Updated code:

function addContact()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var cell = sheet.getActiveCell();
    var active_row = cell.getRow();
    var range = sheet.getDataRange();
  
    var first_name = range.getCell(active_row ,1).getValue();
    var last_name = range.getCell(active_row ,2).getValue();
    var email = range.getCell(active_row,3).getValue();
    var phone = range.getCell(active_row,4).getValue();  
    
    var contact = ContactsApp.createContact(first_name, last_name, email, phone);
    
    var mainGroup = ContactsApp.getContactGroup("System Group: My Contacts");
    
    mainGroup.addContact(contact);
 
  
    showOutputBox('first name' + first_name + '\nlast name' + last_name + '\nemail' + email + '\nphone' + phone, "added contact");
  
}

【问题讨论】:

  • 对不起它的 JavaScript。

标签: javascript google-apps-script google-sheets


【解决方案1】:

问题:

ContactsApp.createContact 只接受三个参数:givenNamefamilyNameemail。没有接受四个参数的createContact 方法。因此你得到的错误。

解决办法:

启用Advanced People Service 以使用People API 并通过people.createContact 创建您的联系人,这允许您设置联系人的电话号码(以及一系列其他属性)。

代码示例:

const optionalArgs = {
  personFields: "emailAddresses,names,phoneNumbers"
};
const resource = {
  "emailAddresses": [
    {
      "value": email
    }
  ],
  "names": [
    {
      "givenName": first_name,
      "familyName": last_name
    }
  ],
  "phoneNumbers": [
    {
      "value": phone
    }
  ]
};
People.People.createContact(resource, optionalArgs);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多