简体   繁体   English

使用Google脚本编辑器添加到电子表格

[英]Adding to spreadsheet using Google Script Editor

I am writing a google script on top of a spreadsheet based on a google form about Auctioning used cars. 我正在基于有关拍卖二手车的google表格的电子表格上编写google脚本。 My teacher gave me these instructions: 我的老师给了我这些指示:

In the spreadsheet add a google script with a function that look for the highest bid and update the Google form field Function 1: Retrieve all the values in the bid column and find the highest value using a loop. 在电子表格中,添加带有功能的google脚本,以查找最高出价并更新Google表单字段功能1:检索出价列中的所有值,并使用循环查找最高值。 Function 2: Open the Google Form and update the form text field (eg Current highest bid is $xxxx, please put in your bid). 功能2:打开Goog​​le表单并更新表单文本字段(例如,当前最高出价为$ xxxx,请输入您的出价)。 Where xxxx is the highest bid. 其中xxxx是最高出价。 (reference: https://developers.google.com/apps-script/reference/forms/ ) Create a trigger in the script that will trigger the above functions and update the Google form. (参考: https : //developers.google.com/apps-script/reference/forms/ )在脚本中创建一个触发器,该触发器将触发上述功能并更新Google表单。

And here is my code: 这是我的代码:

function myFunction(e) {
  var ss = SpreadsheetApp.getActive().getSheetByName('Used Car Auction')
  var form = FormApp.openById('1D0XE9womWv2T_rFYTtc9enQgIhCkxaK8SErxNkTh7NE')
  ScriptApp.newTrigger('onFormSubmit')
  .forForm(form)
  .onFormSubmit()
  .create();
  var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
  var bid = values [88000]
  var highest = Math.max(bid).toString();
  formsetTitle('Used car auction:' + 'The highest bid is:' + highest)
  .setDescription ('Description of Form')
  .setConfirmationMessage('Thanks for bidding!')
  .setAllowResponseEdits(false)
  .setAcceptingResponses(true);
  }

Google says: TypeError: Cannot call method "getSheetByName" of null. Google说:TypeError:无法调用null的方法“ getSheetByName”。

What am I doing wrong? 我究竟做错了什么? I've just coded for five days, and this is confusing me. 我刚编码了五天,这使我感到困惑。

Other than that, am I on the right track, as per the instructions above? 除此之外,按照上述说明,我是否走在正确的轨道上?

There are some fundamental errors in your code. 您的代码中存在一些基本错误。 To write values to a worksheet in Google Spreadsheet you'll have to follow certain steps. 要将值写入Google Spreadsheet中的工作表,您必须执行某些步骤。

Step-1: Get Spreadsheet Object. 步骤1:获取电子表格对象。

eg var spreadsheetObject = SpreadsheetApp.getActiveSpreadsheet(); -> If you are using bounded script.

OR eg var spreadsheetObject = SpreadsheetApp.openById(<SPREADSHEET_ID>); -> If you are using standalone script. eg var spreadsheetObject = SpreadsheetApp.openById(<SPREADSHEET_ID>); -> If you are using standalone script. eg var spreadsheetObject = SpreadsheetApp.openById(<SPREADSHEET_ID>); -> If you are using standalone script.

Step-2: Get Worksheet Object. 步骤2:获取工作表对象。

eg var worksheetObject = spreadsheetObject.getSheetByName(<NAME_OF_WORKSHEET>);

You are getting this TypeError: Cannot call method "getSheetByName" of null error because you are trying to open a worksheet without getting its spreadsheet object. 您将收到此TypeError: Cannot call method "getSheetByName" of null错误的TypeError: Cannot call method "getSheetByName" of null因为您试图在不获取其电子表格对象的情况下打开工作表。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM