function convertToSpeech() { // MP3 file
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settings = ss.getSheetByName('Settings');
const sheet = ss.getSheetByName('AI');
const apiKey = settings.getRange(7,2).getValue();
var text = sheet.getRange(2,2).getValue();
var fileName = 'audio.mp3';
var params = {
input: {
text: text
},
voice: {
languageCode: 'en-US'
, name: 'en-US-Studio-M'
},
audioConfig: {
audioEncoding: 'MP3_64_KBPS'
, effectsProfileId: ['small-bluetooth-speaker-class-device']
, pitch: 0
, speakingRate: 1
}
};
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(params)
});
var json = response.getContentText();
var data = JSON.parse(json);
var audioContent = data.audioContent;
// Convert base64-encoded audio content to binary data
var audioBytes = Utilities.base64Decode(audioContent);
// Save the MP3 file to Google Drive
var file = DriveApp.createFile(fileName, audioBytes, 'audio/mpeg');
Logger.log('Created audio file: ' + file.getName());
}