我正在处理此代码以更新 google 联系人与 google 表单和链接到它的电子表格

I am working on this code to update google contacts with google forms and spreadsheet linked to it

我希望此代码在提交表单时使用触发器自动添加联系人,但出现错误。 该代码适用于电子表格,但我无法使其适用于表单。 我对编码有点菜鸟。 如此简单的解释会有所帮助

联系人也可以添加到 google 联系人中的 "other" 组,有没有办法直接将他们添加到 "my contacts"?

function createHeaders() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  // Freezes the first row
  sheet.setFrozenRows(1);

  // Set the values we want for headers
  var values = [
    ["First Name", "Last Name", "Email", "Phone Number", "Company", "Notes"]
  ];

  // Set the range of cells
  var range = sheet.getRange("A1:F1");

  // Call the setValues method on range and pass in our values
  range.setValues(values);
}

function createContact() {
  var alreadyAdded = "Already added";
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 2;   // Number of rows to process

  // Fetch the range of cells A2:G3
  var dataRange = sheet.getRange(startRow, 1, numRows, 8)

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
    for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var firstName = row[0]
    var lastName = row[1]
    var emailAddress = row[2]
    var phone = row[3]
    var company = row[4]
    var notes = row[5]
    var addedAlready = row[6];

      if (addedAlready != alreadyAdded) {
      // Create contact in Google Contacts
      var contact = ContactsApp.createContact(firstName, lastName, emailAddress);

      // Add values to new contact
      contact.addCompany(company, "");
      contact.addPhone(ContactsApp.Field.WORK_PHONE, phone);
      contact.setNotes(notes);
      sheet.getRange(startRow + i, 7).setValue(alreadyAdded);
      }; 
    };
};

我能够通过将一个或多个空字符串作为参数传递给 createContact() 方法来重现该错误:

var contact = ContactsApp.createContact("", "", "");

通过记录它们来检查数据数组中的值,看看那里是否有任何空字符串。您可以将代码包装在 try 块中,以防止错误停止程序执行。任何错误都将记录在 catch 块中

try {
var contact = ContactsApp.createContact(a, b, c);

}

catch(error) {

Logger.log(error);

}

我看到您正在尝试将电子表格连接到 Google 表单。检查 Connecting to Google Forms

Apps Script allows you to connect Google Forms with Google Sheets through Forms and Spreadsheet services. This feature can automatically create a Google Form based on data in a spreadsheet. Apps Script also enables you to use triggers, such as onFormSubmit to perform a specific action after a user responds to the form.

您可能指的是 onFormSubmit

这是code demo供您参考。