将电话号码与表单元格值匹配到Google通讯录并获取全名和地址

时间:2017-07-08 09:18:49

标签: google-apps-script

这是我的第一个问题,我感到紧张。

在这里,我想通过谷歌联系人获取客户全名和地址。但它不起作用。寻找专家帮助

function importContact() {
   var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Stock" ) { 
    var r = s.getActiveCell();
    var phone = r.getValue()
  if( r.getColumn() == 10 ) {
      var nextCell1 = r.offset(0, 1);
      var nextCell2 = r.offset(0, 2);
      var contacts = ContactsApp.getContactsByPhone(phone,'Other');
   for(var i = 0, iLen = contacts.length; i < iLen; i++) {
    var fullname = contacts[i].getFullName();
    var address = contacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);
    if(fullname) {
       nextCell1.setValue(fullname);
       nextCell2.setValue(address);
    } else {
       nextCell1.setValue('New Customer');
    }
  }  
}
}
}

非常感谢

2 个答案:

答案 0 :(得分:1)

这似乎对我有用。但我没有任何联系人的地址,所以我无法验证。

<!DOCTYPE html>
<?php
if( isset( $_REQUEST['update'] ))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bc140401259_DB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$employeeID="";
if ( ! empty($_POST['empID'])){
    $employeeID = $_POST['empID'];
}
$Name="";
if ( ! empty($_POST['name'])){
    $Name = $_POST['name'];
}
$Address="";
if ( ! empty($_POST['address'])){
    $Address = $_POST['address'];
}
$Dateofbirth="";
if ( ! empty($_POST['DOB'])){
    $Dateofbirth = $_POST['DOB'];
}
$Salary="";
if ( ! empty($_POST['salary'])){
    $Salary = $_POST['salary'];
}
$timestamp="";
if ( ! empty($_POST['timeIn'])){
    $timestamp = $_POST['timeIn'];
}

$sql = "UPDATE  Employee SET Name = $Name, Address = $Address, Dateofbirth = $Dateofbirth, Salary = $Salary, Timein = $timestamp where empID = $employeeID";

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
}

?>

<html>
<body>

<form action="" method="post">
<h2>Add Employee Form</h2>
  Employee ID: <input type="text" name="empID" >
  <br><br>
  Name: <input type="text" name="name" >
  <br><br>

  Address: <input type="text" name="address" >
  <br><br>

  Date of Birth: <input type="text" name="DOB" >
  <br><br>

  Salary: <input type="text" name="salary" >
  <br><br>

  Time In: <input type="text" name="timeIn" >
  <br><br>

<input type="submit" name="update" value="UPDATE" />

</form>
</body>
</html>

答案 1 :(得分:0)

解决方案#1似乎没有完全发挥作用,因此经过调试和修改,直到:

  • 确实确认了“新客户”活动,
  • 为地址工作。

试图评论库珀的一个,但没有足够的声誉。希望这没关系,如果需要,我准备删除我的回复。

关于代码的一些注释:

  • 它假定任何联系人都有名称字段,但可能并不总是如此;
  • 它检查地址字段是否存在,因为它比名称字段更可能是空的;
  • 如果地址不存在,代码将在地址单元格中输入N.A.;
  • var address = addresses[0].getAddress()显然是第一个解决方案中的主要缺失链接;
  • 原始提问者以外的用户应使用相应的工作表名称替换if( s.getName() == "Stock" )中的“Stock”。

现在应该很明显:我是学习者,不是专家。任何改进的建议都非常受欢迎。

function importContact() 
{
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  if( s.getName() == "Stock" )
  { 
    var r = s.getActiveCell();
    var phone = r.getValue();
    if( r.getColumn() == 1 ) 
    {
      var nextCell1 = r.offset(0, 1);
      var nextCell2 = r.offset(0, 2);
      var contacts = ContactsApp.getContactsByPhone(phone);
      if(contacts.length == 0) 
      {
        nextCell1.setValue('New Customer');        
      }
      else 
      {
        for(var i = 0; i < contacts.length; i++) 
        {
          var fullname = contacts[i].getFullName();
          nextCell1.setValue(fullname);
          var addresses = contacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);
          if (addresses.length == 0) 
          {
            nextCell2.setValue("Not Available");
          }
          else
          {
            var address = addresses[0].getAddress() //this was the missing link in the first solution, apparently
            nextCell2.setValue(address);
          }
        }
      }
     }
    }
}