gpt4 book ai didi

javascript - 使用应用程序脚本从 Google Sheets 读取内容并将内容写入 Google Doc

转载 作者:行者123 更新时间:2023-12-03 04:56:20 27 4
gpt4 key购买 nike

我有一个如下所示的表格:

enter image description here

我想从我的 Google 表格中读取该数据,然后将其写入 Google 文档,该文档的内容类似于

Hi James Madison, Welcome to Vienna!
Hi James Monroe, Welcome to Toronto!
Hi John Quincy Adams, Welcome to Paris!
Hi Andrew Jackson, Welcome to New York!
Hi Martin Van Buren, Welcome to London!
Hi William Henry Harrison, Welcome to Tokyo!
Hi John Tyler, Welcome to Berlin!

为此,我知道我需要创建一个脚本和一个模板。

这些句子中的每一个都应该单独存在于一个新文档中,因此它们应该全部收集在同一个文件夹中。

最后我想通过电子邮件将所有文件发送出去。

我一直在查看所有不同的示例,邮件合并等,但即使经过几个小时的搜索和摆弄,我仍然无法弄清楚如何制作这样的脚本。

也许比我更有经验的人可以向我展示如何做到这一点,或者向我指出可以以清晰易懂的方式解释它的资源。

<小时/>

这是我的第一次天真的尝试:

function extractDataFromSheet() {

// Opens spreadsheet by its ID
var spreadsheet = SpreadsheetApp.openById("1ow7iuLoqfCSqVhp3ra2uy-22LHUJfQA2UpZgOvIuQ5c");

// Get the name of this SS
var name_of_sheet = spreadsheet.getName();

var sheet = spreadsheet.getSheetByName('PussyCow'); // or whatever is the name of the sheet
var range = sheet.getRange(1,1);
var data = range.getValue();
//Browser.msgBox(name_of_sheet +" "+data);

// Opens doc by its ID
var doc = DocumentApp.openById("1ZSqN-OOxvDV9YLpuDsXhqSWx4X3djUaVehuIlZbanxo");

var name_of_doc = doc.getName();

var doc_body = doc.getBody();

// Use editAsText to obtain a single text element containing
// all the characters in the document.
var text = doc_body.editAsText();

// Insert text at the beginning of the document.
text.insertText(0, data);

// Insert text at the end of the document.
text.appendText(name_of_sheet);

// Make the first half of the document blue.
text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');


Browser.msgBox(name_of_doc);
}

最佳答案

我不会为您编写整个脚本,但我会向您展示一个示例,说明您可以在释义代码中执行哪些操作(希望这是有意义的)。

因此,您要做的第一件事就是收集所有数据。一个好的方法是:

var ss = SpreadsheetApp.getActive()
var dataList = []
var iteration = ss.getLastRow()
while ( iteration > 0 ) {
dataList.push(ss.getRange('A' + iteration).getValue() + ',' + ss.getRange('B' + iteration).getValue())
iteration--
}

此时,您已获得所有数据的列表。接下来,我们需要将所有这些信息放入单独的谷歌文档中。这可以通过以下方式完成:

var documentList = []
for (i=0;i<dataList.length;i++) {
documentList.push(DocumentApp.create('documentName'))
}
//this is DEFINITELY not the fastest or most effecient way to do it, but whatever
var body = undefined
for (i=0;i<documentList.length;i++) {
body = 'Hi ' + dataList[i].split(',')[0] + ', Welcome to' + dataList[i].split(',')[1] + '!'
documentList[i].getBody.appendParagraph(body)
}

然后,我们想向人们发送电子邮件。您的表格不跟踪电子邮件,所以这是我能做的最好的事情:

 for (i=0;i<documentList.length;i++) {
MailApp.sendEmail('emailAdress','subject','whatever you want the body to be')
}

关于javascript - 使用应用程序脚本从 Google Sheets 读取内容并将内容写入 Google Doc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42421620/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com